git学习笔记

阅读量: searchstar 2022-10-05 16:10:45
Categories: Tags:

remote

查看remote的URL(无需联网):

# https://stackoverflow.com/questions/4089430/how-to-determine-the-url-that-a-local-git-repository-was-originally-cloned-from
git remote get-url origin

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

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 开始^..结束

来源:https://stackoverflow.com/questions/1994463/how-to-cherry-pick-a-range-of-commits-and-merge-them-into-another-branch

变基

git rebase --onto <newbase> <oldbase>

相当于先保存当前的HEAD为oldhead,然后reset --hardnewbase,然后将从oldbase开始(不含)到oldhead(含)的所有commit给逐个cherry-pick过来。

来源:https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase

submodule

# 添加
git submodule add <URL> <path>
# 更新
git submodule update --remote
# 令其reset到repo中指定的commit
# https://stackoverflow.com/questions/7882603/how-to-revert-a-git-submodule-pointer-to-the-commit-stored-in-the-containing-rep
git submodule update --init

删除submodule比较麻烦:

# Remove the submodule entry from .git/config
git submodule deinit -f path/to/submodule

# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/path/to/submodule

# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f path/to/submodule

来源:https://gist.github.com/myusuf3/7f645819ded92bda6677

push到不同名的远程分支

# https://stackoverflow.com/questions/19154302/git-push-to-specific-branch
git push origin localBranchName:remoteBranchName

Squash commits

# -i: --interactive
git rebase <base-commit-hash> -i

然后第一个commit标记为pick,其他的都标记为squash,即s。然后退出编辑器。

然后会让你编辑新的commit的message。可以全删了然后编辑新的message,然后退出编辑器,从原来的HEAD到base commit(不含)的所有commit就都被squash成了一个commit了。

其他

https://stackoverflow.com/questions/89332/how-do-i-recover-a-dropped-stash-in-git

https://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history