我們每天使用 Git ,但是很多命令記不住。
一般來說,日常使用只要記住下圖6個命令,就可以了。但是熟練使用,恐怕要記住60~100個命令。
下面是我整理的常用 Git 命令清單,清單是供大家查缺補漏的,大家可以預覽下以下清單,看自己有哪些是不太熟悉或者不知道的,然後針對性的瞭解其用法。熟練了這些命令可以大大提高效率。
幾個專用名詞的譯名如下。
- Workspace:工作區
- Index / Stage:暫存區
- Repository:倉庫區(或本地倉庫)
- Remote:遠程倉庫
一、新建代碼庫
$ git init
$ git init [project-name]
$ git clone [url]
二、配置
Git的設置文件爲.gitconfig,它可以在用戶主目錄下(全局配置),也可以在項目目錄下(項目配置)。
$ git config --list
$ git config -e [--global]
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"
三、增加/刪除文件
$ git add [file1] [file2] ...
$ git add [dir]
$ git add .
$ git add -p
$ git rm [file1] [file2] ...
$ git rm --cached [file]
$ git mv [file-original] [file-renamed]
四、代碼提交
$ git commit -m [message]
$ git commit [file1] [file2] ... -m [message]
$ git commit -a
$ git commit -v
$ git commit --amend -m [message]
$ git commit --amend [file1] [file2] ...
五、分支
$ git branch
$ git branch -r
$ git branch -a
$ git branch [branch-name]
$ git checkout -b [branch]
$ git branch [branch] [commit]
$ git branch --track [branch] [remote-branch]
$ git checkout [branch-name]
$ git checkout -
$ git branch --set-upstream [branch] [remote-branch]
$ git merge [branch]
$ git cherry-pick [commit]
$ git branch -d [branch-name]
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]
六、標籤
$ git tag
$ git tag [tag]
$ git tag [tag] [commit]
$ git tag -d [tag]
$ git push origin :refs/tags/[tagName]
$ git show [tag]
$ git push [remote] [tag]
$ git push [remote] --tags
$ git checkout -b [branch] [tag]
七、查看信息
$ git status
$ git log
$ git log --stat
$ git log -S [keyword]
$ git log [tag] HEAD --pretty=format:%s
$ git log [tag] HEAD --grep feature
$ git log --follow [file]
$ git whatchanged [file]
$ git log -p [file]
$ git log -5 --pretty --oneline
$ git shortlog -sn
$ git blame [file]
$ git diff
$ git diff --cached [file]
$ git diff HEAD
$ git diff [first-branch]...[second-branch]
$ git diff --shortstat "@{0 day ago}"
$ git show [commit]
$ git show --name-only [commit]
$ git show [commit]:[filename]
$ git reflog
八、遠程同步
$ git fetch [remote]
$ git remote -v
$ git remote show [remote]
$ git remote add [shortname] [url]
$ git pull [remote] [branch]
$ git push [remote] [branch]
$ git push [remote] --force
$ git push [remote] --all
九、撤銷
$ git checkout [file]
$ git checkout [commit] [file]
$ git checkout .
$ git reset [file]
$ git reset --hard
$ git reset [commit]
$ git reset --hard [commit]
$ git reset --keep [commit]
$ git revert [commit]
$ git stash
$ git stash pop
十、其他
$ git archive