Skip to content

Instantly share code, notes, and snippets.

@liuxd
Last active June 23, 2021 23:48
Show Gist options
  • Select an option

  • Save liuxd/122d3f243909d0f8754cf8e3c0f8f2d3 to your computer and use it in GitHub Desktop.

Select an option

Save liuxd/122d3f243909d0f8754cf8e3c0f8f2d3 to your computer and use it in GitHub Desktop.
[Git] #Tips

问:如何统计commit的总数?

答:

git log --oneline|wc -l

问:如何删除本地分支和远端分支?

答:

  • 删除本地分支:git branch -D foo
  • 删除远端分支:git push origin :foo

问:如何修改git默认的编辑器?

答:

git config --global core.editor vim

问:如何修剪本地存储的远端分支列表,确保远端已经删除的分支在本地的远端分支列表也删除掉?

答:

git remote prune origin

问:如何把本地创建的tag推送到远端?

答:

git push --tags

问:如何切换到上一次用的分支?

答:

git checkout -

问:如何把任意的某个提交应用到当前分支上?

答:

git cherry-pick $sha1

问:如果二进制文件冲突了怎么解决冲突?

答:

使用对方的:git checkout --theirs YOUR_BINARY_FILE

使用自己的:git checkout --ours YOUR_BINARY_FILE

如果都不行那就重新做了。。。

问:如何知道两个提交之间修改的文件列表。注:只要文件列表,不要具体修改细节。

答:

两种方式:

  • git diff --stat [$sha1] [$sha1]
  • git diff --name-status [$sha1] [$sha1]

问:如何修改分支名。

答:

git branch -m <old branch name> <new branch name>

问:如何把submodule一起clone下来?

答:

新仓库:

git clone --recursive git://github.com/foo/bar.git

已克隆主库:

git clone git://github.com/foo/bar.git
cd bar
git submodule update --init --recursive

问:如何删除tag?

答:

本地删除:

git tag -d [tagname]

远端删除:

git push [remote] :refs/tags/[tagname]

问:如何获得当前状态的HEAD的sha1码?

答:

git rev-parse HEAD

问:如何正确显示中文?

答:

git默认会对路径里超过0x80的字节进行转码,设置core.quotepathfasle可以禁止自动转码,详情见git-config(1)core.quotepath部分。解决办法是:

git config --global core.quotepath false

问:如何查看和设置分支的上游?

答:

查看:

cat .git/config

设置: ​ # 仅设置上游 git branch --set-upstream-to origin/master

# 推送的同时设置上游
git push -u origin master

问:文件删不掉怎么办?

描述:

有个文件a.php,执行git rm a.php时是正常的,但是一提交就报Could not open input file:a.php

办法:

git rm a.php
touch a.php
git commit -am 'delete a.php'

问:如何给tag改名

git tag -f {new_name} {old_name}
git tag -d {old_name}
git push origin :refs/tags/{old_name}

问:如何制造空提交?

git commit --allow-empty -m 'empty commit'

How to change the commit author for one specific commit?

git commit --amend --author="Author Name <email@address.com>" --no-edit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment