一定要先測試命令的效果後,再用於工作環境中,以防造成不能彌補的後果!到時候別拿著砍刀來找我
所有的命令都在git version 2.7.4(Apple Git-66)下測試通過
統一概念:
工作區:改動(增刪文件和內容)
暫存區:輸入命令:git add 改動的文件名,此次改動就放到了 『暫存區』
本地倉庫(簡稱:本地):輸入命令:git commit 此次修改的描述,此次改動就放到了 』本地倉庫』,每個 commit,我叫它為一個 『版本』。
遠程倉庫(簡稱:遠程):輸入命令:git push 遠程倉庫,此次改動就放到了 『遠程倉庫』(GitHub 等)
commit-id:輸出命令:git log,最上面那行 commit xxxxxx,後面的字符串就是 commit-id
如果喜歡這個項目,歡迎 Star、提交 Pr、反饋問題[3]
git help -g
The command output as below:The common Git guides are:
attributes Defining attributes per path
cli Git command-line interface and conventions
core-tutorial A Git core tutorial for developers
cvs-migration Git for CVS users
diffcore Tweaking diff output
everyday A useful minimum set of commands for Everyday Git
glossary A Git Glossary
hooks Hooks used by Git
ignore Specifies intentionally untracked files to ignore
modules Defining submodule properties
namespaces Git namespaces
repository-layout Git Repository Layout
revisions Specifying revisions and ranges for Git
tutorial A tutorial introduction to Git
tutorial-2 A tutorial introduction to Git: part two
workflows An overview of recommended workflows with Git
'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept.
git fetch --all && git reset --hard origin/master
也就是把所有的改動都重新放回工作區,並清空所有的 commit,這樣就可以重新提交第一個 commit 了。git update-ref -d HEAD
輸出工作區和暫存區的 different(不同)。git diff
還可以展示本地倉庫中任意兩個 commit 之間的文件變動:git diff <commit-id> <commit-id>
輸出暫存區和本地最近的版本(commit)的 different(不同)。git diff --cached
輸出工作區、暫存區 和本地最近的版本(commit)的 different(不同)。git diff HEAD
git checkout -
git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d
git branch -vv
關聯之後,git branch -vv 就可以展示關聯的遠程分支名了,同時推送到遠程倉庫直接:git push,不需要指定遠程倉庫了。git branch -u origin/mybranch
git push origin/mybranch -u
git branch -r
git branch -a
git checkout -b <branch-name>
git checkout -b <branch-name> origin/<branch-name>
git branch -d <local-branchname>
git push origin --delete <remote-branchname>
git push origin :<remote-branchname>
git branch -m <new-branch-name>
git tag
git describe --tags --abbrev=0
git tag -ln
git tag <version-number>
默認 tag 是打在最近的一次 commit 上,如果需要指定 commit 打 tag:$ git tag -a <version-number> -m "v1.0 發布(描述)" <commit-id>
首先要保證本地創建好了標籤才可以推送標籤到遠程倉庫:git push origin <local-version-number>
git push origin --tags
git tag -d <tag-name>
刪除遠程標籤需要先刪除本地標籤,再執行下面的命令:git push origin :refs/tags/<tag-name>
一般上線之前都會打 tag,就是為了防止上線後出現問題,方便快速回退到上一版本。下面的命令是回到某一標籤下的狀態:git checkout -b branch_name tag_name
git checkout <file-name>
git checkout .
git rev-list -n 1 HEAD -- <file_path> #得到 deleting_commit
git checkout <deleting_commit>^ -- <file_path> #回到刪除文件 deleting_commit 之前的狀態
以新增一個 commit 的方式還原某一個 commit 的修改git revert <commit-id>
回到某個 commit 的狀態,並刪除後面的 commit和 revert 的區別:reset 命令會抹去某個 commit id 之後的所有 commitgit reset <commit-id> #默認就是-mixed參數。
git reset –mixed HEAD^ #回退至上個版本,它將重置HEAD到另外一個commit,並且重置暫存區以便和HEAD相匹配,但是也到此為止。工作區不會被更改。
git reset –soft HEAD~3 #回退至三個版本之前,只回退了commit的信息,暫存區和工作區與回退之前保持一致。如果還要提交,直接commit即可
git reset –hard <commit-id> #徹底回退到指定commit-id的狀態,暫存區和工作區也會變為指定commit-id版本的內容
如果暫存區有改動,同時也會將暫存區的改動提交到上一個 commitgit commit --amend
git log
git blame <file-name>
每次更新了 HEAD 的 Git 命令比如 commint、amend、cherry-pick、reset、revert 等都會被記錄下來(不限分支),就像 shell 的 history 一樣。這樣你可以 reset 到任何一次更新了 HEAD 的操作之後,而不僅僅是回到當前分支下的某個 commit 之後的狀態。git reflog
git commit --amend --author='Author Name <email@address.com>'
git remote set-url origin <URL>
git remote add origin <remote-url>
git remote
git whatchanged --since='2 weeks ago'
把 A 分支的某一個 commit,放到 B 分支上這個過程需要 cherry-pick 命令,參考[4]。git checkout <branch-name> && git cherry-pick <commit-id>
git config --global alias.<handle> <command>
比如:git status 改成 git st,這樣可以簡化命令
git config --global alias.st status
git stash
git stash -u
git stash list
git stash apply <stash@{n}>
回到最後一個 stash 的狀態,並刪除這個 stashgit stash pop
git stash clear
git checkout <stash@{n}> -- <file-path>
git ls-files -t
git ls-files --others
git ls-files --others -i --exclude-standard
可以用來刪除新建的文件。如果不指定文件文件名,則清空所有工作的 untracked 文件。clean 命令,注意兩點:clean 後,刪除的文件無法找回
不會影響 tracked 的文件的改動,只會刪除 untracked 的文件
git clean <file-name> -f
可以用來刪除新建的目錄,注意:這個命令也可以用來刪除 untracked 的文件。詳情見上一條。git clean <directory-name> -df
git log --pretty=oneline --graph --decorate --all
git bundle create <file> <branch-name>
新建一個分支,分支內容就是上面 git bundle create 命令導出的內容git clone repo.bundle <repo-dir> -b <branch-name>
git rebase --autostash
git fetch origin pull/<id>/head:<branch-name>
git diff --word-diff
git clean -X -f
注意:config 分為:當前目錄(local)和全局(golbal)的 config,默認為當前目錄的 configgit config --local --list(當前目錄)
git config --global --list(全局)
git status --ignored
commit 歷史中顯示 Branch1 有的,但是 Branch2 沒有 commitgit log Branch1 ^Branch2
git log --show-signature
git config --global --unset <entry-name>
新建並切換到新分支上,同時這個分支沒有任何 commitgit checkout --orphan <branch-name>
git show <branch-name>:<file-name>
git clone -b <branch-name> --single-branch https://github.com/user/repo.git
關閉 track 指定文件的改動,也就是 Git 將不會在記錄這個文件的改動git update-index --assume-unchanged path/to/file
git update-index --no-assume-unchanged path/to/file
git config core.fileMode false
git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/
通過 grep 查找,given-text:所需要查找的欄位git log --all --grep='<given-text>'
git reset <file-name>
git push -f <remote-name> <branch-name>
type:commit 的類型
feat:新特性
fix:修改問題
refactor:代碼重構
docs:文檔修改
style:代碼格式修改,注意不是 css 修改
test:測試用例修改
chore:其他修改,比如構建流程,依賴管理
scope:commit 影響的範圍,比如:route,component,utils,build……
subject:commit 的概述
body:commit 具體修改內容,可以分為多行
footer:一些備註,通常是 BREAKING CHANGE 或修復的 bug 的連結
使用Commitizen代替 git commit可以使用 cz-cli[7] 工具代替 git commit。npm install -g commitizen cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc