git简单使用

1、已经跟踪且提交的文件,取消跟踪:
git rm -r --cached "path/to/foo/" #文件夹
git rm -r --cached "path/to/file" #文件

2、git 如何用某一分支的某文件,替换另一分支的某文件:
git checkout branch_name -- file_name

2.1 后来加的:
git diff -U3 --word-diff --color-words #美化了的diff,比较清爽


3、多人开发时,如果出现同时修改某个文件,则需要merge,只要配置了mergetool,则直接在终端敲入:git mergetool 即可merge

4、忽略多层文件夹用**,如: **/bin/Debug/, 前面的两个**号代表任意多层上级文件夹
5、git log/whatchanged 只查看最近2条commits:
git log -n 2

        git log 查看两个时间之间的提交:

git log --before="2022-05-8T00:00:00" --after="2022-05-07T00:00:00"

  git log 查看某个用户的提交:

git log --author="\(Adam\)\|\(Jon\)"
上面的参数可以复合在一起使用。  可以用 whatchanged 替换 log。

6、查看某行代码或者某关键字是什么时候添加的,最后一次修改是什么时候:
6.1, 当前的commit下,某些行最后一次的修改记录:
git blame -L ${start_lineno},${end_lineno} ${filename}

       示例1:  git blame -L 783,783 client/main.c

  示例2:  git blame client/main.c                #该文件的所有行

6.2,某关键字什么时候添加的(即第一次出现的时候):
git log -S searchTerm                #搜索searchTerm在所有文件中的第一次出现,只列出第一个出现的文件
git log -S searchTerm test.txt           #搜索searchTerm在test.txt文件中的第一次出现
git log --all --color -p --reverse --source -S 'searchTerm'     #搜索searchTerm在所有文件中的第一次出现,列出所有文件,且用 git diff 显示他们的区别,先提交的 commits 显示在前(--reverse).

可以加入alias:

$ git config --global alias.find '!git log --color -p --reverse -S '   #!必须要加上,不然参数无法传递给-S, --color and -p helps to show exactly "whatchanged"
$ git find <whatever>
$ git find <whatever> --all
$ git find <whatever> master develop

  6.3, 查看某些行的所有修改历史:

# git log --topo-order --color --graph -u -L 1,1:client/main.c

6.4  某文件的修改历史查看:

a, 文件的提交历史: git log -- <file>     or       git log --follow -- <file>

      --follow 表示不管文件重命名,但是这里却能列出文件重命名的更改历史。参看 d,更详细。相比a,更推荐使用d。

b,若a不够详细,可以查看提交历史及更改细则(diff): git log -p -- <file>

c,   若b不够详细,可以查看提交历史及更改细则(diff)及重命名历史:  git log --follow -p -- <file>

d,  不看diff,只看更改状态,则可以使用   git log --oneline -M --stat --follow --  <file>

      其中,-M就是查看重命名:-M/--find-renames。

e,查看两个提交之间的文件更改(结果为文件列表): git diff --name-only <SHA/tag> <SHA/tag>,       其中sha为commit的sha

        示例: 最后3次提交的文件更改: git diff --name-only HEAD HEAD~3     or         git diff --name-status HEAD HEAD~3

 

 

  add alias:

[alias]
    # Follow evolution of certain lines in a file
    # arg1=file, arg2=first line, arg3=last line or blank for just the first line
    follow = "!sh -c 'git log --topo-order --color -u -L $2,${3:-$2}:"$1"'" -

 

  示例: git follow client/main.c 2 3



git tag 使用:
git tag                # 查看当前分支下的标签
git tag -l 'v0.1.*'          # 搜索符合当前模式的标签
git tag v0.2.1-light          # 创建轻量标签
git tag -a v0.2.1 -m '0.2.1版本'  # 创建附注标签
git checkout [tagname]        # 切换到标签
git show v0.2.1            # 查看标签版本信息
git tag -d v0.2.1          # 删除标签
git tag -a v0.2.1 9fbc3d0      # 补打标签
git push origin v0.1.2        # 将v0.1.2标签提交到git服务器
git push origin --tags        # 将本地所有标签一次性提交到git服务器

 

# git 查看当前分支某人的所有提交:
git log --author="Jon"       # 会匹配到所有包含 Jon 的人名,如Jon Smith和Jon Adan
git log --author="Jon Smith"

# git 查看当前分支某几个人的提交:
git log --author="\(Adam\)\|\(Jon\)"

# 不光是 log,还可以 whatchanged 命令,也可以 log --oneline 等等很多命令,都可以筛选某个人 # 查看所有分支的提交请加上
--all

 

 




我自己的配置文件:~/.gitconfig:
[user]
    name = hzh
    email = 924948@qq.com

[core]
    editor = vi
    quotepath = false

[merge]
    tool = kdiff3
[mergetool "kdiff3"]
    #path = E:/playground/softwares/KDiff3/kdiff3.exe
    keepBackup = false
    trustExitCode = false

[diff]
    tool = kdiff3
[difftool "kdiff3"]
    #path = E:/playground/softwares/KDiff3/kdiff3.exe
    keepBackup = false
    trustExitCode = false

[alias]
    co = commit
    br = branch
    ch = checkout
    st = status
    last = log -1 HEAD

[color]
    diff = auto
    status = auto
    branch = auto

[push]
    default = matching

 

posted @ 2015-03-03 16:09  微信公众号--共鸣圈  阅读(259)  评论(0编辑  收藏  举报