您好,登錄后才能下訂單哦!
設置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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。