[关闭]
@muyanfeixiang 2015-03-18T08:48:50.000000Z 字数 4420 阅读 1470

Git Notes

git

常用操作

1.使用分支

新建分支
git checout -b dev(分支名)相当于 git branch dev(新建) git checkout dev(switch branch)
git branch : show all the branches
git merge dev: merge dev to the current branch
Git鼓励大量使用分支:
查看分支:git branch
创建分支:git branch <name>
切换分支:git checkout <name>
创建+切换分支:git checkout -b <name>
合并某分支到当前分支:git merge <name>
删除分支:git branch -d <name>
git log --graph: to show the graph of branch merge
获取远程分支git fetch
运行 git fetch,可以将远程分支信息获取到本地,再运行 git checkout -b local-branchname origin/remote_branchname 就可以将远程分支映射到本地命名为local-branchname 的一分支。
查看远程分支 git branch -r
将本地分支推送到远程 git push origin local:remote
//其中local代表本地分支,remote代表要推送到的远程分支
我们gerrit 推送 就是git push origin local: refs/for/branch%r=reviewer
就必须创建远程origin的dev分支到本地: git checkout -b dev origin/dev pull the remote branch dev to the local

设置dev和origin/dev的链接: git branch --set-upstream dev origin/dev,然后在 git pull
查看远程库信息,使用git remote -v: to check the branch infomation of the remote repository
git remote : show the remote branch
删除远程一开始用
git branch -r -d origin/branch-name

不成功,发现只是删除的本地对该远程分支的track,正确的方法应该是这样:

git push origin :branch-name(不用添加refs/for)
冒号前面的空格不能少,原理是把一个空分支push到server上,相当于删除该分支。

2.git diff

git diff 无参数 比较staging area 和working area
git diff master 比较master分支和working area
git diff HEAD   比较HEAD指向的内容和working area
git diff  refs/for/master 比较远程master分支和当前工作区
git diff branch1 branch2  *.cshtml 比较branch1和branch2下的 cshtml文件
如果明确指出比较的两个分支,第二个是目标分支,
+   public GetTicketInvoke(string json)
+   public GetTicketInvoke(string json)
+号代表是在目标分支出现,原分支没有
-号代表原分支里面有,目标没有

如果只指定一个分支,则其实原分支,当前工作区是目标分支
git diff这个命令能比较两个提交之间的差异,使用–name-only参数可以只显示文件名

diff里面a表示前面那个变量,b表示第二个变量

3.创建Tag

git tag <name>: create a tag at HEAD,and you can also specify a commit id
git tag -a <tagname> -m "tag infomation" : give some tag information
git tag -s <tagname> -m "blablabla..."可以用PGP签名标签;
git tag : to show all tags
git tag -d <tagname> :delete the local tag
git push origin <tagname> : push tag to the remote
git push不会推送标签(tag),除非使用--tags选项。
git push origin --tags : push all the local tags
git push origin :refs/tags/<tagname>shi : delete the remote tag

4.解决冲突

首先从远程服务器 git fetch ssh://liuyb@code.ctripcorp.com:29418/test refs/changes/12/7412/1 && git checkout FETCH_HEAD 迁出上一次提交版本的分支

在本地 git checkout -b solve_conflict 新建一个解决冲突的分支
git pull origin master
获取最新的master 会提示 conflict
From ssh://code.ctripcorp.com:29418/test
* branch master -> FETCH_HEAD
e0a6fa8..af83aa2 master -> origin/master
Auto-merging test_liu.txt
CONFLICT (content): Merge conflict in test_liu.txt
Automatic merge failed; fix conflicts and then commit the result.
如下
hello git
first test
<<<<<<< HEAD
haha,conflict
=======
this is first commit
this is test amend
>>>>>>>> 1486741435e2161e3311746a3750901a8fae0e5c

然后在本地解决冲突。
git push origin HEAD:refs/for/master

5.忽略特殊文件

在.gitignore 文件里配置
for example

# Windows:
Thumbs.db
ehthumbs.db
Desktop.ini

# Python:
*.py[cod]
*.so
*.egg
*.egg-info
dist
build

6.配置别名

git config --global alias.st status :
then you can type in "git st " instead of git status

The other example
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch

--global参数是全局参数,也就是这些命令在这台电脑的所有Git仓库下都有用。

在撤销修改一节中,我们知道,命令git reset HEAD file可以把暂存区的修改撤销掉(unstage),重新放回工作区。既然是一个unstage操作,就可以配置一个unstage别名:

$ git config --global alias.unstage 'reset HEAD'
当你敲入命令:

$ git unstage test.py
实际上Git执行的是:

$ git reset HEAD test.py
配置一个git last,让其显示最后一次提交信息:

$ git config --global alias.last 'log -1'
这样,用git last就能显示最近一次的提交:

$ git last
commit adca45d317e6d8a4b23f9811c3d7b7f0f180bfe2
Merge: bd6ae48 291bea8
Author: Michael Liao
Date: Thu Aug 22 22:49:22 2013 +0800

甚至还有人丧心病狂地把lg配置成了:
用于显示各个branch分支和merge情况
lg1 = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold

yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
lg = !"git lg1"

7.使用rebase

8.保存现场

git stash
查看stash列表,git stash list
恢复工作现场
git stash pop stash@{num}。num 就是你要恢复的工作现场的编号
如果想要清空stash队列则使用git stash clear。
同时注意使用git stash pop命令是恢复stash队列中的stash@{0}即最上层的那个工作现场。而且使用pop命令恢复的工作现场,其对应的stash 在队列中删除。使用git stash apply stash@{num}方法除了不在stash队列删除外其他和git stash pop 完全一样。

9.复位,撤销修改,暂存,提交

git reflog 查看所有commit
git checkout -- filename将会删除该文件中所有没有暂存和提交的改动,这个操作是不可逆的
git reset --hard HEAD^ 强制复位前一个提交, 慎用,不可恢复
git reset HEAD可以用来清除已经add到缓存区但是不想进一步提交的内容。
git reset HEAD benchmarks.rb
另一种方式是用git rm --cached x.txt 表示取消git对x.txt的跟踪,但是不删除文件
注意:git rm -f --cached a.txt 与 git rm -f a.txt 之间的区别: 前者保留工作区的文件,后者是全部删除。
补充提交: git commit --amend,补充提交只更新提交说明

  1. git log -p filename

查看文件的每一个详细的历史修改,如果没有-p选项,只显示提交记录,不显示文件内容修改,git log -p -3 filename 显示最近的3次提交。

  1. git log --pretty=oneline filename

每一行显示一个提交,先显示哈希码,再显示提交说明。

  1. git blame filename

查看文件的每一行是哪个提交最后修改的。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注