- git回退指定个commit
- git同步空文件夹
- git解决sign_and_send_pubkey: signing failed: agent refused operation
- git checkout 远程分支error
- 免密码用https协议clone private github repo
¶ 查看unstaged changes
git diff |
包含untracked file:
git add -N . && git diff |
来源:https://stackoverflow.com/a/857696/13688160
¶ 取消add
git reset |
来源:https://stackoverflow.com/questions/348170/how-do-i-undo-git-add-before-commit
¶ remote
查看remote的URL(无需联网):
https://stackoverflow.com/questions/4089430/how-to-determine-the-url-that-a-local-git-repository-was-originally-cloned-from |
¶ clone
指定分支
git clone -b 分支名 仓库地址 |
¶ tag
显示全部:git tags
创建:git tag 名字
推送:git push origin 名字
删除:git tag -d 名字
如果remote删除那么本地也删除:git fetch origin --prune
删除所有本地tag: git tag | xargs git tag -d
来源:https://stackoverflow.com/a/19542426
¶ 删除未跟踪的文件
预览有哪些未跟踪的文件
git clean -dn |
删除未跟踪的文件
git clean -df |
来源:https://koukia.ca/how-to-remove-local-untracked-files-from-the-current-git-branch-571c6ce9b6b1
¶ stash
暂存更改:
git stash |
列出所有stash:
git stash list |
应用并删除最近的stash:
git stash pop |
删除指定stash:
git stash drop stash@{0} |
查看指定stash:
输出类似于git commit |
用patch的格式打印指定stash:
-p: --patch |
可以保存为patch:
-p: --patch |
使用patch:
git apply --3way /path/to/xxx.patch |
--3way
: https://stackoverflow.com/a/47756467/13688160
¶ 恢复某个被删除的stash
git log --graph --oneline --decorate $(git fsck --no-reflog | awk '/dangling commit/ {print $3}') |
找到了之后:
git stash apply stash的哈希 |
来源:https://stackoverflow.com/questions/89332/how-do-i-recover-a-dropped-stash-in-git
¶ git log
¶ 查看所有commit的列表
git log |
¶ 查看某文件的commit历史
git log --all --full-history -- <path-to-file> |
P.S. 可以是已经删除的文件。路径是相对shell的工作目录的相对路径。
来源:https://stackoverflow.com/questions/7203515/how-to-find-a-deleted-file-in-the-project-commit-history
¶ 查看某一个commit的diff
git show <commit-id> |
原文:https://stackoverflow.com/questions/1157818/how-can-i-show-what-a-commit-did
¶ cherry-pick
¶ 指定commit
git cherry-pick CommitHash |
指定冲突解决方案:--strategy-option
。如果在冲突时全部采用cherry-pick过来的commit的更改,则--strategy-option theirs
。
来源:https://stackoverflow.com/questions/21051850/force-git-to-accept-cherry-picks-changes
¶ 一段连续的commit
git cherry-pick 开始..结束 |
注意,开始
是不包含在内的,而结束
是包含在内的,也就是说这是个左开右闭区间。如果要让开始
也被包含在内,即指定一个闭区间,则:
git cherry-pick 开始^..结束 |
¶ 变基
git rebase --onto <newbase> <oldbase> |
相当于先保存当前的HEAD为oldhead
,然后reset --hard
到newbase
,然后将从oldbase
开始(不含)到oldhead
(含)的所有commit给逐个cherry-pick过来。
来源:https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase
¶ submodule
添加 |
更改submodule的URL:直接编辑.gitmodules
,然后
git submodule sync --recursive |
删除submodule比较麻烦:
Remove the submodule entry from .git/config |
来源:https://gist.github.com/myusuf3/7f645819ded92bda6677
¶ push到不同名的远程分支
https://stackoverflow.com/questions/19154302/git-push-to-specific-branch |
¶ Squash commits
-i: --interactive |
然后第一个commit标记为pick
,其他的都标记为squash
,即s
。然后退出编辑器。
然后会让你编辑新的commit的message。可以全删了然后编辑新的message,然后退出编辑器,从原来的HEAD到base commit(不含)的所有commit就都被squash成了一个commit了。
¶ 恢复某一被删除的文件
先用前面介绍的方法查看这个文件的commit历史:
git log --all --full-history -- <path-to-file> |
找到删除这个文件的commit hash,然后checkout这个文件到它的前一个commit:
git checkout <deletion commit hash>~1 -- <filename> |
来源:https://www.git-tower.com/learn/git/faq/restoring-deleted-files
¶ 把某个文件回退到某个版本
git checkout c5f567 -- file1/to/restore file2/to/restore |
来源:https://stackoverflow.com/a/215731/13688160
¶ 其他
https://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history