git学习笔记

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

查看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
git remote get-url origin

clone

指定分支

git clone -b 分支名 仓库地址
# 如果只需要这个分支的话
git clone -b 分支名 --single-branch 仓库地址

参考:git clone 指定分支

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
git stash show stash@{0} 

用patch的格式打印指定stash:

# -p: --patch
git stash show stash@{0} -p

可以保存为patch:

# -p: --patch
git stash show stash@{0} -p > xxx.patch

使用patch:

git apply --3way /path/to/xxx.patch

--3way: https://stackoverflow.com/a/47756467/13688160

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

来源: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了。

恢复某一被删除的文件

先用前面介绍的方法查看这个文件的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

其他

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