设置pull时候rebase
$ git config branch.master.rebase true
$ git config --global branch.autosetuprebase always
远程分支删除后本地更新
git remote prune origin
git命令介绍
一、git clone
第一种:git clone <repository> <directory> 克隆的相当于工作区。
第二种:git clone --bare <repository> <directory> 克隆的是祼版本库,gerrit 和gitlab上获取项目用的就是这个命令。
第三种:git clone --mirror <repository> <directory> 也是祼版本库,区别于第二种是,对上游版本进行了注册,可以用git fetch命令持续同步。
git clone ssh://admin@10.10.159.210:29418/rrx_java_common
二、git add
git add .
git add -A [<path>]表示把<path>中所有tracked文件中被修改过或已删除文件和所有untracted的文件信息添加到索引库。
git add file //这种用法是比较推荐的
三、git commit
git commit -m "msg"
git commit --amend //对上一次提交的补充,不生成新的提交id(如果是gerrit,也不生成changeId);
git commit -C commit //引用commit 的提交信息。
四、git push <远程主机名> <本地分支名>:<远程分支名>
4.1 git push origin master:master //是指将本地分支master推送到远程(origin)master分支上,当第一次提交项目,这是必须的。也可以写成git push origin master
4.2 git push origin tagName //提交tag
例子:
git tag A -m "add a"
git push origin A
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 454 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 2 (delta 0)
remote: Processing changes: refs: 1, done
To ssh://yinzhibin@10.10.159.210:29418/test_gerrit
* [new tag] A -> A
==============================
4.3 git push origin --delete tagName 删除tag //gitlib 好用,gerrit目前不能用
例子:
git push origin --delete A
To https://git.oschina.net/ygnup/alert.git
- [deleted] A
=================================
4.4 git push origin branchName:branchName //提交分支branchName到分支上去
例子:
git branch mtest
bogon:test_gerrit yin197$ git push origin mtest:mtest
Total 0 (delta 0), reused 0 (delta 0)
remote: Processing changes: closed: 53, refs: 1, done
To ssh://yinzhibin@10.10.159.210:29418/test_gerrit
* [new branch] mtest -> mtest
=================================
4.5 git push origin :branchName 或 git push origin --delete branchName //删除分支, //gitlib 好用,gerrit目前不能用
例子:
git branch -a
a_t
* master
remotes/origin/HEAD -> origin/master
remotes/origin/m
remotes/origin/master
bogon:alert yin197$ git push origin :m
To https://git.oschina.net/ygnup/alert.git
- [deleted] m
=================================
五、git reset
1.git reset [-q] [<commit>] [--] <paths> ...
如果包含路径,不会重置引用,更不会改变工作区,而是用指定的提交状态(<commit>)下的文件 替换掉暂存区的文件。
例子:
git rev-parse HEAD origin/master
b7d32903e360f9d57983c2506ee950ff9334680c
b7594472f3e4a34129277ebf74ca5fd520daba70
bogon:test_gerrit yin197$ git reset b7594472f3e4a34129277ebf74ca5fd520daba70 -- a
Unstaged changes after reset:
M a
2.git reset [--soft | --maxed | --hard | --merge | --keep ] [-q] [<commit> ]
3.git reset commitId .替换引用指向,替换缓存区。
例子:
git rev-parse HEAD origin/master
105f41859d7d0099370d54bcfc284eca30cd04a3
b7594472f3e4a34129277ebf74ca5fd520daba70
bogon:test_gerrit yin197$ git status
On branch master
Your branch is ahead of 'origin/master' by 5 commits.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
bogon:test_gerrit yin197$ git reset b7594472f3e4a34129277ebf74ca5fd520daba70
Unstaged changes after reset:
M a
D b
D c
D d
D e
bogon:test_gerrit yin197$ git rev-parse HEAD origin/master
b7594472f3e4a34129277ebf74ca5fd520daba70
b7594472f3e4a34129277ebf74ca5fd520daba70
bogon:test_gerrit yin197$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: a
deleted: b
deleted: c
deleted: d
deleted: e
no changes added to commit (use "git add" and/or "git commit -a")
bogon:test_gerrit yin197$ ls
a
4.git reset --soft commitId ,只替换引用。
例子:
bogon:test_gerrit yin197$ git status
On branch master
Your branch is ahead of 'origin/master' by 5 commits.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
bogon:test_gerrit yin197$ git rev-parse HEAD master origin/master
2b6bb7a810c8a8a89ea928b0e9e79914a98b902e
2b6bb7a810c8a8a89ea928b0e9e79914a98b902e
b7594472f3e4a34129277ebf74ca5fd520daba70
bogon:test_gerrit yin197$ git reset --soft b7594472f3e4a34129277ebf74ca5fd520daba70
bogon:test_gerrit yin197$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: a
deleted: b
deleted: c
deleted: d
deleted: e
bogon:test_gerrit yin197$ ls
a
bogon:test_gerrit yin197$ git rev-parse HEAD master origin/master
b7594472f3e4a34129277ebf74ca5fd520daba70
b7594472f3e4a34129277ebf74ca5fd520daba70
b7594472f3e4a34129277ebf74ca5fd520daba70
5. git reset --hard commitId ,替换引用指向,替换缓存区,替换工作区。
例子:
bogon:test_gerrit yin197$ ls
a
bogon:test_gerrit yin197$ git rev-parse HEAD master origin/master
b7594472f3e4a34129277ebf74ca5fd520daba70
b7594472f3e4a34129277ebf74ca5fd520daba70
b7594472f3e4a34129277ebf74ca5fd520daba70
bogon:test_gerrit yin197$ git reset --hard origin/master
HEAD is now at b759447 add e1
bogon:test_gerrit yin197$ ls
a b c d e
六、git checkout
1. git checkout [<commit>] [--] <path> ...
如果 commit 省略,相当于从暂存区中获取数据,替换工作区的指定文件。不会改变头指针。
如果 不省略commit,相当于从commit 拣出分支中的文件,替换掉暂存区和工作区。不会改变头指针。
例子:
bogon:test_gerrit yin197$ git reset --hard origin/master
HEAD is now at b759447 add e1
bogon:test_gerrit yin197$ ls
a b c d e
bogon:test_gerrit yin197$ git reset --hard 2b6bb7a810c8a8a89ea928b0e9e79914a98b902e
HEAD is now at 2b6bb7a ma
bogon:test_gerrit yin197$ ls
a
bogon:test_gerrit yin197$ git checkout b7594472f3e4a34129277ebf74ca5fd520daba70 -- b
bogon:test_gerrit yin197$ ls
a b
2.git checkout [<branch>]
会改变头指针,之所以后会写成branch是因为只有是分支,HEAD才会被跟踪,否则会进入“分离头指针”。分离头指针下,
提交不能被引用关联,可能丢失。所以这种用法主要是为了切换分支。
例子:
bogon:test_gerrit yin197$ git branch -a
good
* master
mb
mtest
remotes/origin/HEAD -> origin/master
remotes/origin/good
remotes/origin/master
remotes/origin/mtest
remotes/origin/my_branch_gerrit
bogon:test_gerrit yin197$ ls
a b
bogon:test_gerrit yin197$ git status
On branch master
Your branch is ahead of 'origin/master' by 5 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: b
bogon:test_gerrit yin197$ git checkout good
Switched to branch 'good'
bogon:test_gerrit yin197$ ls
a b c d e
bogon:test_gerrit yin197$ git status
On branch good
nothing to commit, working directory clean
======================================
3.git checkout [-b] <new_branch> [<start_point>]
该用法主要是创建分支 new_branch,以start_point创建提交开始,同时切换到new_branch分支上。
七、git stash
git stash //用于保存当前工作区文件和暂存区文件状态到内存中,以便于执行其他分支任务,待其他任务执行完成后,可恢复。
git stash pop //恢复上次保存的工作进度。
例子:
bogon:test_gerrit yin197$ git checkout good
Switched to branch 'good'
bogon:test_gerrit yin197$ ls
a b c d e
bogon:test_gerrit yin197$ git status
On branch good
nothing to commit, working directory clean
bogon:test_gerrit yin197$ ls
a b c d e
bogon:test_gerrit yin197$ vim f
bogon:test_gerrit yin197$ git add f
bogon:test_gerrit yin197$ echo good >> f
bogon:test_gerrit yin197$ git status
On branch good
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: f
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: f
bogon:test_gerrit yin197$ git stash
Saved working directory and index state WIP on good: 2ac3c1d m e
HEAD is now at 2ac3c1d m e
bogon:test_gerrit yin197$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 5 commits.
(use "git push" to publish your local commits)
bogon:test_gerrit yin197$ ls
a
bogon:test_gerrit yin197$ git checkout mb
Switched to branch 'mb'
bogon:test_gerrit yin197$ ls
a b c d e
bogon:test_gerrit yin197$ git checkout good
Switched to branch 'good'
bogon:test_gerrit yin197$ ls
a b c d e
bogon:test_gerrit yin197$ git stash pop
On branch good
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: f
Dropped refs/stash@{0} (c9c49262bdef9cfe1382c87d3a2b117ed7ebeb0d)
========================================================
八、git fetch 和 git pull
git fetch <==> git fetch origin +refsheads/*:refs/remote/origin/* //将远程版本库的所有分支复制为本地远程分支。
git fetch origin/master //从远程的origin的master主分支下载最新的版本到origin/master分支上
//只会覆盖本地 origin/master 分支,本地master ,HEAD 引用都不发生变化。
//这一点在理解用rebase命令时非常有用。
bogon:test_gerrit yin197$ git rev-parse HEAD master origin/master
2ac3c1d721439c4684477351bd8a00da3ee546e4
2b6bb7a810c8a8a89ea928b0e9e79914a98b902e
b7594472f3e4a34129277ebf74ca5fd520daba70
bogon:test_gerrit yin197$ git fetch
bogon:test_gerrit yin197$ git rev-parse HEAD master origin/master
2ac3c1d721439c4684477351bd8a00da3ee546e4
2b6bb7a810c8a8a89ea928b0e9e79914a98b902e
b7594472f3e4a34129277ebf74ca5fd520daba70
bogon:test_gerrit yin197$ git fetch origin master
From ssh://10.10.159.210:29418/test_gerrit
* branch master -> FETCH_HEAD
bogon:test_gerrit yin197$ git rev-parse HEAD master origin/master
2ac3c1d721439c4684477351bd8a00da3ee546e4
2b6bb7a810c8a8a89ea928b0e9e79914a98b902e
b7594472f3e4a34129277ebf74ca5fd520daba70
git pull origin/master = git fetch origin/master + git merge origin/master
九、git rebase 和 git merge [[http://gitbook.liuhui998.com/4_2.html]]
git rebase --onto newbase since till
基于newbase 提交,从 since提交 但不包括since 到 till提交做一个顺序的重复提交。
相当于 以下几个步骤:
1.git checkout newbase //相当于git reset --hard newbase
2.将since 到 till的所有版本在newbase依次提交,不包含since,但包含till.
如果遇到冲突会中止,先解决冲突,再提交。再执行git rebase --continue
直至完成。
例子:
bogon:test_gerrit yin197$ git log --oneline -10
fadb845 add e
ef2c6a6 add d
e18d3e3 add c
839b69f add b
2b6bb7a ma
fe70ae9 mj
b7d3290 ma
b369e44 add a
a7b8d45 del abcde
b759447 add e1
bogon:test_gerrit yin197$ git rebase --onto B C E
First, rewinding head to replay your work on top of it...
Applying: add d
Applying: add e
bogon:test_gerrit yin197$ git log --oneline -10
f6bb914 add e
f986796 add d
839b69f add b
2b6bb7a ma
fe70ae9 mj
b7d3290 ma
b369e44 add a
a7b8d45 del abcde
b759447 add e1
39988d5 add e
bogon:test_gerrit yin197$
十、git log
git log --oneline --decorate //可以看到提交对应的tag及其他引用。
git log --graph --pretty=oneline | head -5 //会显示和分支合并情况
git log --pretty=oneline -5 只显示5行。
git reflog --oneline | head -5 //查看当前引用的记录
十一、git branch
git branch -r //查看远程分支
git show-ref //查看所有本地分支
git ls-remote //查看远程分支
git branch -a 查看所有分支
git branch -D br //强制删除本地分支
git push origin the_branch //向远程推送分支
git push origin :the_branch //删除远程 the_branch,在分git上可用,gerrit上现在有问题。
git branch newbranch //创建分支
git checkout -b newbranch master <==> git branch newbranch && git checkout master
git branch -m oldbranch newbranch //修改分支名称,如果有其他分支引用,则修改失败。这时可以用 -M参数
git branch -M oldbranch newbranch //强制修改分支名称
git push origin mb:mb //向远程推送分支
十二、git tag
git tag //显示tag列表
git tag tag_name -m "tag msg" //打tag
git tag -a production_v6 -m '说明' : 打一个标签 , 作为记录
git push origin tag tag_name //推送到远程服务器 也可以用 git push origin tag_name
git tag -D tag_name //删除本地tag
git push origin --delete tag tag_name //删除远程tag
git ls-remote --tags : 列出服务器所有的标签
十三、git remote
git remote -v //查看origin 地址
git remote update //更新远程版本库
git remote prune origin //刷新本地远程分支,当本地分支在远程不存在时,会被清空。[[http://www.cnblogs.com/wangiqngpei557/p/6058115.html?utm_source=tuicool&utm_medium=referral]]
git remote show origin //来查看有关于origin的一些信息,包括分支是否tracking
十四、git rev-parse
查看指定分支的提交Id ,如:git rev-parse HEAD master origin/master
十五、git config
以下两个命令任何位置都可以,是全局的。
git config --global user.name "zhangsan"
git config --global user.email "123456@qq.com"
以下命令只能在项目内部使用
git config remote.origin.push refs/heads/*:refs/for/*
git config branch.<branchname>.rebase true // 为分支设置git pull 命令执行的是 git rebase命令。
bogon:test_gerrit yin197$ git config --global branch.autosetuprebase always
bogon:test_gerrit yin197$ git pull
First, rewinding head to replay your work on top of it...
Applying: add a
Applying: ma
Applying: mj
Applying: ma
Applying: add b
Applying: add c
Applying: add d
Applying: add e
意思是git push 命令相当于执行:
git push 相当于执行: git push refs/heads/*:refs/for/* ,是将本地分支 refs/heads/* 的分支,提交到远程分支 refs/for/*
也可以编辑文件:git config -e --global
十六、git cherry-pick
git cherry-pick <commit> //用指定分支与当前分支自动合并,如果不冲突,生成新的提交id,如果冲突,解决冲突,手动提交,生成新的提交id.
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。