git tips

how to pull after forced update

1
git reset origin/master --hard

this willResets the index and working tree. Any changes to tracked files in the working tree since are discarded.

当前工作目录的index和本地修改,都会被丢弃(丢弃的修改包括已经提交到本地的commit)

可以使用 --soft If you want to actually keep whatever changes you’ve got locally, do a --soft reset instead. which will update the commit history for the branch, 不会丢弃本地未提交的修改,但是会丢弃本地提交的修改。

另外还有两个参数 --keep --merge,我在本地试了一下,都报错了,没有深入了解。

git rebase

You can replay your local commits ontop of any other commit/branch using git rebase 例如你在feature分支,你可以基于master分支对当期的分支rebase

1
git rebase -i origin/master

this will invoke rebase in interactive mode where you can choose how to apply each individual commit that isn’t in the history you are rebasing on top of.

你也可以基于当期分支的之前的commit进行分子

1
git rebase -i HEAD~3

相对于从倒退3个提交,开始rebase,这样你就可以对的多次提交进行合并。

如果你的分支已经push到了远端,那么在rebase之后,你要push的话,只能用 forced git push。

git pull after forced update

Throw away local commits in git

前面讲到的git reset就可以完成这个工作。例如你在master分支上

1
git reset --hard origin/master

这样就可以达到需要的效果。

不要使用git revert, git revert会提交一个新的commit去revert原来的commit,这样在history中就可以看到。

git clean

http://stackoverflow.com/questions/61212/how-do-i-remove-local-untracked-files-from-my-current-git-branch

1
git clean -f

But beware… there’s no going back. Use -n or --dry-run to preview the damage you’ll do.

If you want to also remove directories, run git clean -f -d or git clean -fd

If you just want to remove ignored files, run git clean -f -X or git clean -fX

If you want to remove ignored as well as non-ignored files, run git clean -f -x or git clean -fx

Note the case difference on the X for the two latter commands.

If clean.requireForce is set to “true” (the default) in your configuration, then unless you specify -f nothing will actually happen.

See the git-clean docs for more information.

git cache

for git cache on linux, the following command can be used:

1
git config credential.helper 'store [options]'

the details, please refer to: git-credential-store

git cache on windows

“This [git-credential-cache] doesn’t work for Windows systems as git-credential-cache communicates through a Unix socket.”

  • For msysgit 1.8.1 and above

The wincred helper was added in msysgit 1.8.1. Use it as follows:

1
git config --global credential.helper wincred

For msysgit versions older than 1.8.1, please refer to: git: ‘credential-cache’ is not a git command

git ignore已经在代码库中的文件

如果已经在代码库中提交了文件,但是本地有修改,又不希望提交到代码库中,例如maven中的local配置,每个人都不一样,都要修改,但是修改以后又不希望提交到代码库中。

忽略本地修改(config.php为例子文件):

1
git update-index --assume-unchanged config.php

在ignore中添加后,git status还是会出现,还可以通过在代码库中,删除这个文件的缓存,可以用这个命令:

1
git rm --cached config.php

注意这个命令,如果push以后会在远程代码库中删除文件的。

git在linux的终端中的自动补全

如果你在命令行中使用Git命令,每次手动输入命令是一件非常烦人的。为了解决这个问题,你可以很方便的开启自动补全功能。 在Unix系统下,运行以下指令来获取脚本:

1
2
cd ~
wget https://raw.github.com/git/git/master/contrib/completion/git-completion.bash --no-check-certificate -O ~/.git-completion.bash

然后,在您的~/.bash_profile文件中添加以下代码:

1
2
3
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi

上面这种方法只是在当前用户中添加了git的自动补全。

如果需要为所有的用户添加自动补全的功能,将git-completion.bash拷贝到mac的/opt/local/etc/bash_completion.d目录下,或者linux的/etc/bash_completion.d/的目录下,bash将自动加载这些目录下的脚本。

在windows系统中,已经添加了自动补全,所以不需要了。

reference

让你的Git水平更上一层楼的10个小贴士 Git Basics - Tips and Tricks