Usage

  • 中文文件名乱码
1
git config --global core.quotepath false
  • 创建新分支
1
2
git fetch
git checkout -b local-branchname origin/remote_branchname
  • 删除远程分支
1
git push origin --delete branchName
  • 放弃更改
1
git checkout -f
  • 放弃未提交的更改:
1
git reset --hard
  • 放弃最近的 merge
1
git reset --hard HEAD~
  • 放弃未跟踪的文件及目录:
1
git clean -fd
  • 查看单个文件历史
1
git log --pretty=oneline 文件名
  • 查看命令历史
1
git reflog
  • 查看工作区和版本库里面最新版本
1
git diff HEAD -- filename
  • 丢弃工作区的修改 回到最近一次 git commit 或 git add 时的状态
1
git checkout -- file
  • 暂存区的修改回退到工作区
1
git reset HEAD file
  • 版本回退
1
git reset --hard commit_id
  • 修改 commit 记录(push 要 force)
1
git rebase -i <commit>
  • 添加远程仓库:
1
git remote add origin git@hostname:aaa.git
  • 删除远程仓库
1
git remote rm origin
  • 修改远程仓库
1
git remote set-url origin <URL>
  • 第一次 push 需要:
1
git push -u origin master
  • 比较分支
1
2
3
git diff topic master # 将两个分支上最新的提交做 diff,相当于 diff 了两个 commit
git diff topic..master # 同上
git diff topic...master # 输出自 topic 与 master 分别开发以来,master 分支上的 change
  • git checkout 命令加上-b 参数表示创建并切换

  • 分支的合并情况

1
git log --graph --pretty=oneline --abbrev-commit
  • 合并分支时,加上–no-ff 参数就可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而 fast forward 合并就看不出来曾经做过合并。
  • 当手头工作没有完成时,先把工作现场 git stash 一下,然后去修复 bug,修复后,再 git stash pop,回到工作现场。

  • 如果要丢弃一个没有被合并过的分支,可以通过 git branch -D name 强行删除。

  • git push origin branch-name 推送自己的修改

  • 如果本地分支和远程分支的链接关系没有创建,用命令 git branch –set-upstream branch-name origin/branch-name

  • 打标签

1
2
git tag tag_name
git tag tag_name commit_id
  • git tag 标签不是按时间顺序列出,而是按字母排序的

  • 用私钥签名一个标签

1
git tag -s tag_name -m "signed version 0.2 released" commit_id
  • 推送标签
1
git push origin tag_name
  • 标签附注
1
2
git tag -a v1.4 -m 'my version 1.4'
git show v1.4
  • 推送所有标签
1
git push origin --tags
  • 删除远程标签:
1
2
git tag -d v0.9
git push origin :refs/tags/v0.9
  • 重置分支
1
2
3
4
5
git log
git reset --hard 7b78c88
git push -f
git merge go
git push
  • 重命名分支
1
2
3
git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch # Delete the old branch
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
1
2
3
java -jar bfg-1.12.12.jar --strip-blobs-bigger-than 100M [仓库目录]
java -jar bfg-1.12.12.jar --delete-files id\_{dsa,rsa} [仓库目录]
git gc --prune=now --aggressive
  • git 拒绝合并无关的历史纪录
1
git pull origin master --allow-unrelated-histories
  • 删除文件 使用 git filter-branch 替换历史,然后 –force push。

比如把写有密码的 Rakefile 不小心 push 到 Github 上的话,做以下操作即可。

1
2
$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch Rakefile' --prune-empty --tag-name-filter cat -- --all
$ git push origin master --force
  • submodule
1
2
3
4
5
6
# add
git submodule add https://github.com/saturn-xiv/hugo-bootstrap.git themes/bootstrap
upgrade source code
git submodule update --remote --merge
cd themes/bootstrap
git push --recurse-submodules=check