git 常用命令集合

1、gerrit 提交審核 git push origin HEAD:refs/for/master
2、不改變chang-id的情況下,修改上一次推送 git commit --amend
每個change-id對應(yīng)一個review,可以手動修改chang-id提交覆蓋上一版本的提交

3、以圖形化方式顯示版本合并歷史 git log --graph --pretty=oneline --abbrev-commit
--- 用vscode配合Gitlens 插件能夠更好地以圖形化的方式顯示提交歷史等信息。

4、默認情況下合并分支后,如果被合并的分支被刪除則不會保留分支的信息,可以在merge的時候加上 --no-off 參數(shù)禁用 Fast Forward。
例:
git merge --no-ff -m "merge log" dev

5、將代碼回退到指定版本
git reset --hard xxxxxxxxxxxxxxxxxxxxxxxx

git diff xxxxx > patch
git apply patch

合并提交:
git rebase -i HEAD~2
更新到某個tag
git checkout -b new_branch tag

6、生成change-id
git rebase

7、
git log --graph --pretty=oneline --abbrev-commit

8、Git合并某個分支的一個cherry-pick到另一個分支
例如要將A分支的一個commit合并到B分支:
首先切換到A分支
git checkout A
git log
找出要合并的commit ID :
例如 0128660c08e325d410cb845616af355c0c19c6fe
然后切換到B分支上
git checkout B
git cherry-pick 0128660c08e325d410cb845616af355c0c19c6fe
然后就將A分支的某個commit合并到了B分支了

轉(zhuǎn)移多個提交
Cherry pick 支持一次轉(zhuǎn)移多個提交。
git cherry-pick <HashA> <HashB> 上面的命令將 A 和 B 兩個提交應(yīng)用到當前分支。這會在當前分支生成兩個對應(yīng)的新提交。 如果想要轉(zhuǎn)移一系列的連續(xù)提交,可以使用下面的簡便語法。 git cherry-pick A..B
上面的命令可以轉(zhuǎn)移從 A 到 B 的所有提交。它們必須按照正確的順序放置:提交 A 必須早于提交 B,否則命令將失敗,但不會報錯。
注意,使用上面的命令,提交 A 將不會包含在 Cherry pick 中。如果要包含提交 A,可以使用下面的語法。
$ git cherry-pick A^..B

9、從庫里刪除但是不物理刪除
git rm --cached xxxx

10、更新并合并
git pull --rebase
原理:把本地當前分支的未推送 commit 臨時保存為補丁 (patch)(這些補丁放到 .git/rebase 目錄中),然后將遠程分支的代碼拉取到本地,最后把保存的這些補丁再應(yīng)用到本地當前分支上。
若要把 rebase 當做 git pull 的預(yù)設(shè)值,可以修改 ~/.gitconfig 讓所有 tracked branches 自動使用該設(shè)定:
[branch]
autosetuprebase = always

11、Windows下面使用git拉取或者提交項目時,遇到長路徑提示file name too long的解決方案
可以使用以下命令來修復:
git config --system core.longpaths true

也可以僅設(shè)置當前項目:
git config core.longpaths true

查看設(shè)置狀態(tài):
git config core.longpaths

(git-cheatsheet_Page_2.png-26b069-1526711607289-0)

git-cheatsheet_Page_2.png

12、若要查看已暫存的將要添加到下次提交里的內(nèi)容,可以用 git diff --cached 命令。
(Git 1.6.1 及更高版本還允許使用 git diff --staged,效果是相同的,但更好記些。)

13、Git Diff 的插件版本
可以使用 git diff 來分析文件差異。 但是,如果你喜歡通過圖形化的方式或其它格式輸出方式的話,可以使用 git difftool 命令來用 Araxis ,emerge 或 vimdiff 等軟件輸出 diff 分析結(jié)果。 使用 git difftool --tool-help 命令來看你的系統(tǒng)支持哪些 Git Diff 插件。

14、刪除branch
git branch -d/D xxxxx

15、重命名本地branch
git branch -m xxxxxx

16、新建本地分支并推送到遠程
git checkout -b test-branch
git push origin test-branch:test-branch
本地新建branch,推送到gerrit上:
確保推送的commit id 在庫中可以找到,然后再在此基礎(chǔ)上增加commit。

17、和遠程庫同步刪除的tag
git fetch --prune --tags
向遠程庫推送tag
git push origin <nameOfYourTag>
刪除本地庫tag
git tag -d tag_name

18、
refs/changes/[CD]/[ABCD]/[EF]
Where:
[CD] is the last two digits of the change number
[ABCD] is the change number
[EF] is the patch set number

You can use the change reference to fetch its corresponding commit:

git fetch https://[GERRIT_SERVER_URL]/[PROJECT] refs/changes/[XX]/[YYYY]/[ZZ] \ && git checkout FETCH_HEAD

Your ref spec in repo can be read with: git ls-remote | grep $(git rev-parse HEAD).

19、git always says "Warning: Permanently added '...' (RSA) to the list of known hosts."
解決辦法:create a ~/.ssh/config file and insert the line:

UserKnownHostsFile ~/.ssh/known_hosts

20、git add
Summary:

git add -A stages all changes

git add . stages new files and modifications, without deletions

git add -u stages modifications and deletions, without new files

21、git設(shè)置默認編輯為vim

If you want to set the editor only for Git, do either (you don’t need both):

  • Set core.editor in your Git config: git config --global core.editor "vim"
  • Set the GIT_EDITOR environment variable: export GIT_EDITOR=vim

22、sync delete tags with remote repo:
git fetch origin --prune --tags

push only the desired TAG:
git push origin <tag name>

23、推送特定的一個commit到遠程庫
git push <remotename> <commit SHA>:<remotebranchname> works. the trick is to combine it with git rebase -i to move the commit you want as the first commit, and specify that commit-sha.

24、git stash -p
交互式stash部分文件

25、解決沖突,應(yīng)用某個版本:
從一個特定分支獲取文件拷貝(比如說你在合并master和feature123兩個分支):

git checkout master flash/foo.fla # 或者... git checkout feature132 flash/foo.fla
# 然后... git add flash/foo.fla

另一種方式是通過git輸出文件 - 你可以輸出到另外的文件名,然后當你決定了要用哪個后,再將選定的正確文件復制為正常的文件名:

git show feature132:flash/foo.fla > feature132-foo.fla # 檢出master-foo.fla和feature132-foo.fla
# 假如說我們決定來自feature132的文件是正確的 mv feature132-foo.fla flash/foo.fla
$ git add flash/foo.fla

也可以用“git checkout —ours flash/foo.fla”和“git checkout —theirs flash/foo.fla”來檢出特定版本的文件,而不用記住你在合并的分支名字。

26、git reflog --date=iso

27、git shortlog -sn 命令可以列出代碼倉庫的提交者統(tǒng)計

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。