git简单使用

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

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

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

4、忽略多层文件夹用**,如: **/bin/Debug/, 前面的两个**号代表任意多层上级文件夹

5、查看某行代码或者某关键字是什么时候添加的,最后一次修改是什么时候:
5.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                #该文件的所有行

  5.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).
$ 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

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

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

  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





我自己的配置文件:~/.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




原文地址:https://www.cnblogs.com/welhzh/p/4311223.html