title: "Git in Real World" date: 2021-06-08T15:45:30+07:00 draft: false
$git fetch --all --tags
$git checkout tags/<tag_name> -b <new_branch_name>
# 删除本地 tag
$git tag -d <tag_name>
# 删除远程 tag
$git push origin :refs/tags/<tag_name>
# 删除远程 tag 2
$git push --delete origin <tag_name>
$git config --global core.autocrlf true
$git config --global core.whitespace cr-at-eol
$git config --global core.editor vim
$git log --pretty=oneline [--oneline] <filename> # 更新的文件列表
$git log -p <filename> # 详情
$git show <commit> # 显示文件 diff
$git show --stat --oneline <sha> # 仅显示文件和统计信息
$git show --name-only --oneline <sha> # 仅显示文件名
$git show --stat --oneline stash@{1} # 查看 stash
$git reset HEAD
$git reset HEAD <filename>
$git reset HEAD <path>
$git rm --cached <filename>
$git reset --soft HEAD^
# 如果撤销 2 次 commit
$git reset --soft HEAD~2
# 其他参数
--mixed 为默认参数,不删除工作空间代码改动,撤销 commit,并撤销 add
# $git reset --mixed HEAD~2 == $git reset HEAD~2
--soft 不删除工作空间代码改动,撤销 commit, 不撤销 add
--hard 删除工作空间代码改动,撤销 commit,撤销 add
假设情景如下: 有一些本地分支,其中 master 是用来同步的主分支 对应自己有一个远程仓库 origin, origin 是 clone 自另一个仓库 假设地址在 https://github.com/a/b.git. 现在需要拉取原始仓库的更新
$git remote add upstream https://github.com/a/b.git
$git fetch upstream
$git checkout master
$git rebase upstream/master
$git push -f origin master
$git checkout -- .
$git clean -d -n # dry run
$git clean -d -f # remove untracked files
$git clean -d -i # interactive mode
$git clean -d -f <folder> # remove untracked files in folder
$git clean -d -f -x # ignore files included.
# cancel the merge
$git reset --hard HEAD
# merge by theirs
$git pull --strategy=theirs <remote>
or
$git checkout <branchA>
$git merge -X theirs <branchB>