git使用总结

git branch的使用方法:

1、创建本地分支 local_branch
git branch local_branch

2、创建远程分支remote_branch对应的本地分支local_branch, 并切换到local_branch分支
git checkout -b local_branch remote_branch


3、切换到分支local_branch
git checkout local_branch


4、推送本地分支local_branch到远程分支 remote_branch并建立关联关系
a.远程已有remote_branch分支并且已经关联本地分支local_branch且本地已经切换到local_branch
git push

b.远程已有remote_branch分支但未关联本地分支local_branch且本地已经切换到local_branch
git push -u origin/remote_branch

c.远程没有remote_branch分支并,本地已经切换到local_branch
git push origin local_branch:remote_branch

5、删除本地分支local_branch
git branch -d local_branch

6、删除远程分支remote_branch
git push origin :remote_branch
git branch -d | -D branchname 删除branchname分支
git branch -d -r branchname 删除远程branchname分支

7、重命名分支
git branch -m | -M oldbranch newbranch 重命名分支,如果newbranch名字分支已经存在,则需要使用-M强制重命名,否则,使用-m进行重命名。

8、查看本地分支
git branch

9、查看远程和本地分支
git branch -a


10、查看远程分支
git branch -r

11、查看远程分支和本地分支的对应关系
git branch -vv

12、 批量删除分支

git branch  | cut -c3- | egrep "^3.2" | xargs git branch -D
  ^                ^                ^         ^ 
  |                |                |         |--- create arguments
  |                |                |              from standard input
  |                |                |
  |                |                |---your regexp 
  |                |
  |                |--- skip asterisk 
  |--- list all 
       local
       branches

 

git tag的使用方法

Git 中的tag指向一次commit的id,通常用来给开发分支做一个标记,如标记一个版本号。
1.打标签
git tag -a v1.01 -m "Relase version 1.01"
注解:git tag 是打标签的命令,-a 是添加标签,其后要跟新标签号,-m 及后面的字符串是对该标签的注释。

2.提交标签到远程仓库

git push origin v0.1.2 # 将v0.1.2标签提交到git服务器
git push origin --tags # 将本地所有标签一次性提交到git服务器
注解:就像git push origin master 把本地修改提交到远程仓库一样,-tags可以把本地的打的标签全部提交到远程仓库。

3.删除标签
git tag -d v1.01
注解:-d 表示删除,后面跟要删除的tag名字

4.删除远程标签
git push origin :refs/tags/v1.01
注解:就像git push origin :branch_1 可以删除远程仓库的分支branch_1一样, 冒号前为空表示删除远程仓库的tag。

5.查看标签
git tag
或者
git tag -l

git stash的使用方法

git stash save xxxx 暂存代码,可以把本地一些不需要提交的改动存在stash,每次取完代码,使用git stash apply
git stash list 存储列表
git stash apply stash@{0} 使用暂存0

查看代码暂存,代码提交历史tig .
查看stash list:打开tig,输入h显示帮助,可看到输入y。

 

git svn的使用方法

1.update code
git svn fetch 获取服务器的最新代码
git svn rebase 合并当前代码和服务器的代码
2.commit code
1. update code 通步骤1
2.git merge
3.git commit -s /git commit --amend
3.git svn dcommit

git 合并分支方法

平时可以用git checkout -b 创建新的分支进行修改,修改后merge到主分支
使用--no-ff参数后,会执行正常合并,在Master分支上生成一个新节点。为了保证版本演进的清晰,我们希望采用这种做法。
git checkout master
git merge --no-ff develop

git 查看文件的修改历史

查看单个文件的改动历史:方法1: tig XXX 文件名 方法2:gitk [filename](注意没有.)
查看某行的改动历史:git blame -L 30,40 XX文件名

比较两次提交:

git diff这个命令能比较两个提交之间的差异,使用–name-only参数可以只显示文件名
git diff commit-id1 commit-id2 --name-only

 

git配置:

git修改签名(Signed-off-by):

       git config --global user.name "Your Name Comes Here"
    git config --global user.email you@yourdomain.example.com
git其他配置:
  git config --global color.ui auto
  git config --global alias.st status
  git config --global alias.ci commit
  git config --global alias.co checkout
  git config --global alias.br branch
git config --global alias.lol "log --graph --decorate --pretty=oneline --abbrev-commit"
git config --global core.editor vim

 

git忽略文件

如果某些文件已经被跟踪了, 再放入到.gitinore可能会失效, 用以下命令来忽略
git update-index --assume-unchanged filename

撤销用:
git update-index --no-assume-unchanged filename

 

git中文文件名乱码解决方法

通过将Git配置变量 core.quotepath 设置为false,就可以解决中文文件名称在这些Git命令输出中的显示问题
$ git config --global core.quotepath false

 

git打patch

1)两个节点之间的提交: git format-patch 节点A 节点B
2)单个节点: git format-patch -1 节点A (-n就表示要生成几个节点的提交)
3)最近一次提交节点的patch :git format-patch HEAD^ 依次类推……
使用git format-patch命令生成的patch文件,包含了提交的附加信息:比如作者,时间等。再次基础上使用git am命令即可将此补丁应用到当前分支。注意应用完之后,你会发现当前分支多了一次提交记录,并且有完整的信息,而不是简单的修改文件

posted @ 2015-12-31 16:46  自由飞翔2012  阅读(173)  评论(0编辑  收藏  举报