轉載:https://blog.csdn.net/cherishhere/article/details/52606884
轉載:https://blog.zengrong.net/post/1746.html
轉載:https://blog.csdn.net/xinghuowuzhao/article/details/78663526
轉載:https://blog.csdn.net/boysky0015/article/details/78185879/
0,檢視遠端分支
git remote -v
1.建立分支
git branch develop
2.檢視本地分支:
git branch

注:名稱前面加* 号的是目前的分支
3.檢視遠端分支:
加上-a參數可以檢視遠端分支,遠端分支會用紅色表示出來(如果你開了顔色支援的話)
git branch -a
4.切換分支
git checkout branch_name
5.删除本地分支
git branch -d branch_name
6.删除遠端分支
git branch -r -d origin/branch-name
git push origin :branch-name
7.如果遠端建立了一個分支,本地沒有該分支。
可以利用 git checkout --track origin/branch_name ,這時本地會建立一個分支名叫 branch_name ,會自動跟蹤遠端的同名分支 branch_name。
git checkout --track origin/branch_name
8.如果本地建立了一個分支 branch_name,但是在遠端沒有。
這時候 push 和 pull 指令就無法确定該跟蹤誰,一般來說我們都會使其跟蹤遠端同名分支,是以可以利用 git push --set-upstream origin branch_name ,這樣就可以自動在遠端建立一個 branch_name 分支,然後本地分支會 track 該分支。後面再對該分支使用 push 和 pull 就自動同步。
git push --set-upstream origin new_branch_name
or 遠端有:
git branch --set-upstream debug origin/debug
9.合并分支到master上
首先切換到master分支上
git checkout master
如果是多人開發的話 需要把遠端master上的代碼pull下來
git pull origin master
然後我們把dev分支的代碼合并到master上
git merge dev
然後檢視狀态
git status
檢視本地分支與遠端分支的關聯
三選一
git branch -vv
git remote show origin
cat .git/config
git push報錯:
git push
fatal: The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use
git push origin HEAD:7-3-x
To push to the branch of the same name on the remote, use
git push origin HEAD
To choose either option permanently, see push.default in 'git help config'.
遠端與本地的分支名相同時,才能省略成 git push。否則必須寫全!!!
git 如何同步本地、遠端的分支和tag資訊
git 如何在本地同步遠端分支和tag
1.git如何同步本地分支與遠端origin的分支
問題場景1:
同僚A建立了本地分支branchA并push到了遠端->同僚B在本地拉取(git fetch)了和遠端branchA同步的本地分支branchA->同僚A開發完成将遠端分支branchA删除(遠端倉庫已經不存在分支branchA)->同僚B用git fetch同步遠端分支,git branch -r發現本地仍然記錄有branchA的遠端分支
分析:遠端有新增分支,git fetch可以同步到新的分支到本地,但是遠端有删除分支,直接"git fetch"是不能将遠端已經不存在的branch等在本地删除的
解決方法:
git fetch --prune #這樣就可以實作在本地删除遠端已經不存在的分支
或者簡略:
git fetch -p
git fetch -p origin
git remote prune origin
branch常用的指令:
git branch -a #檢視本地和遠端所有的分支
git branch -r #檢視所有遠端分支
git branch #檢視所有本地分支
git branch -d -r origin/branchA #删除遠端分支
其他較常用的指令
git fetch #将本地分支與遠端保持同步
git checkout -b 本地分支名x origin/遠端分支名x #拉取遠端分支并同時建立對應的本地分支
問題場景2:
本地分支提示:Git Your branch is ahead of ‘origin/master’ by X commits,你想讓本地直接和遠端保持同步,想讓不再提示這個讨厭資訊,那麼如果你本地的commit確定不想要,可以如下操作:
1)git reset --hard origin/master
或者還有一個将本地代碼與伺服器代碼更新一緻的語句
2)git branch -u origin/master
如果想直接回退版本讓遠端和本地代碼保持一緻,那就確定本地代碼沒問題之後強制推到遠端
git push -f origin master
2.git 如何同步本地tag與遠端tag
問題場景:
同僚A在本地建立tagA并push同步到了遠端->同僚B在本地拉取了遠端tagA(git fetch)->同僚A工作需要将遠端标簽tagA删除->同僚B用git fetch同步遠端資訊,git tag後發現本地仍然記錄有tagA
分析:對于遠端repository中已經删除了的tag,即使使用git fetch --prune,甚至"git fetch --tags"確定下載下傳所有tags,也不會讓其在本地也将其删除的。而且,似乎git目前也沒有提供一個直接的指令和參數選項可以删除本地的在遠端已經不存在的tag(我目前是沒找到有關這類tag問題的git指令~~,有知道的同學可以告知我下,互相進步)。
git tag -l | xargs git tag -d #删除所有本地分支
git fetch origin --prune #從遠端拉取所有資訊
#查詢遠端tags的指令如下:
git ls-remote --tags origin
tag常用git指令:
git tag #列出所有tag
git tag -l v1.* #列出符合條件的tag(篩選作用)
git tag #建立輕量tag(無-m标注資訊)
git tag -a -m ‘first version’ #建立含标注tag
git tag -a f1bb97a(commit id) #為之前送出打tag
git push origin --tags #推送所有本地tag到遠端(場景:有兩倉庫。A,B. 先同步了。後來A更新了很多tag,這時B就落後了。把A pull出來,遠端指向B,做pull和push。發現送出都同步到了B.但是Tag沒有同步。)
git push origin #推送指定本地tag到遠端
git tag -d #删除本地指定tag
git push origin :refs/tags/ #删除遠端指定tag
git fetch origin #拉取遠端指定tag
git show #顯示指定tag詳細資訊
參考閱讀
http://smilejay.com/2013/04/git-sync-tag-and-branch-with-remote/
https://blog.zengrong.net/post/1746.html
GIT本地建立分支并送出到遠端倉庫
1.建立本地倉庫
檢視目前項目根目錄中有沒有 .git檔案(隐藏檔案),如果沒有,右鍵->Git bash here ,然後輸入指令git init建立本地倉庫
git init
2.将代碼送出到本地倉庫
git add .
git commit -m "new branch commit"
3.在本地倉庫中建立一個與遠端倉庫的别名,以便之後送出代碼而不是每次都要輸入遠端倉庫位址。指令結尾是git的倉庫位址。
git remote add origin https://github.com/lioms/lioms.git
4.此時我要把本地的代碼送出的遠端倉庫上,步驟如下
1)首先要建立本地的分支,并切換到該分支上(本地建立完分支,預設是在master分支上)
git branch hello_git_branch
git checkout hello_git_branch
2)push到遠端倉庫上面
git push origin hello_git_branch
由于剛才我們為遠端倉庫起了一個别名,那麼這裡就可以使用别名origin調用。
這裡的含義是将hello_git_branch這個分支送出到遠端倉庫上面。如果遠端倉庫沒有這個分支,那麼也會建立一個該分支。
當然,也可以指定送出到遠端倉庫的某個分支上。
如下,是将hello_git_branch分支送出到遠端倉庫的master上面
git push origin hello_git_branch:master
拓展:
如果本地目前是在hello_git_branch分支上面,此時想把遠端倉庫的master與我的hello_git_branch分支合并(merge),可以使用如下指令:
git pull origin hello_git_branch:master
如果您使用如下指令,含義就是将遠端倉庫的master分支合并下來。如果本地沒有master分支,那麼本地就建立一個master分支;如果有這個分支就是 fetch + merge 操作。
git pull master
git remote -v
git branch develop
git branch

git branch -a
git checkout branch_name
git branch -d branch_name
git branch -r -d origin/branch-name
git push origin :branch-name
git checkout --track origin/branch_name
git push --set-upstream origin new_branch_name
or 遠端有:
git branch --set-upstream debug origin/debug
git checkout master
git pull origin master
git merge dev
git status
三選一
git branch -vv
git remote show origin
cat .git/config
git push
fatal: The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use
git push origin HEAD:7-3-x
To push to the branch of the same name on the remote, use
git push origin HEAD
To choose either option permanently, see push.default in 'git help config'.
git init
git add .
git commit -m "new branch commit"
git remote add origin https://github.com/lioms/lioms.git
git branch hello_git_branch
git checkout hello_git_branch
git push origin hello_git_branch
git push origin hello_git_branch:master
git pull origin hello_git_branch:master
git pull master
git init
git add .
git commit -m "new branch commit"
git remote add origin https://github.com/lioms/lioms.git
git branch hello_git_branch
git checkout hello_git_branch
git push origin hello_git_branch
git push origin hello_git_branch:master
git pull origin hello_git_branch:master
git pull master