對git分支不了解,今天測試使用git
分支,本地創建了一個 new
分支,commit
後push
報錯了,且遠端並沒有看到new
分支。
遠端截圖bitbucket.org,只看到預設的master分支,看不到new分支
Welcome to Git (version 1.9.5-preview20141217)
Run 'git help git' to display the help index.
Run 'git help <command>' to display help for specific commands.
Administrator@NSGRZQBMHMHTYQF /E
$ mkdir test
Administrator@NSGRZQBMHMHTYQF /E
$ cd test
Administrator@NSGRZQBMHMHTYQF /E/test
$ touch "d" > 1.txt
Administrator@NSGRZQBMHMHTYQF /E/test
$ ls
1.txt d
Administrator@NSGRZQBMHMHTYQF /E/test
$ git init
Initialized empty Git repository in e:/test/.git/
Administrator@NSGRZQBMHMHTYQF /E/test (master)
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
1.txt
d
nothing added to commit but untracked files present (use "git add" to track)
Administrator@NSGRZQBMHMHTYQF /E/test (master)
$ git add --all
Administrator@NSGRZQBMHMHTYQF /E/test (master)
$ git commit -m "head version"
[master (root-commit) 22a9e38] head version
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 1.txt
create mode 100644 d
Administrator@NSGRZQBMHMHTYQF /E/test (master)
$ git remote add origin git@bitbucket.org:myacc/br.git
Administrator@NSGRZQBMHMHTYQF /E/test (master)
$ git push -u origin --all # pushes up the repo and its refs for the first time
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 214 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@bitbucket.org:myacc/br.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
Administrator@NSGRZQBMHMHTYQF /E/test (master)
$ git branch new
Administrator@NSGRZQBMHMHTYQF /E/test (master)
$ git checkout new
Switched to branch 'new'
Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ git status
On branch new
nothing to commit, working directory clean
Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ touch "2" > 2.txt
Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ git status
On branch new
Untracked files:
(use "git add <file>..." to include in what will be committed)
2
2.txt
nothing added to commit but untracked files present (use "git add" to track)
Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ git add --all
Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ git status
On branch new
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: 2
new file: 2.txt
Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ git commit -m "branch new head version"
[new 4429a97] branch new head version
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 2
create mode 100644 2.txt
Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ git push -u origin:new
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.
In Git 2.0, Git will default to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
ssh: Could not resolve hostname origin: no address associated with name
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ git status
On branch new
nothing to commit, working directory clean
1.創建了分支之後,再 git add .
就添加了,然後git commit -m ""
就提交到了當前的分支?
2.如果運行了 git checkout new
,那麼是不是會從遠端下載new分支到本地,直接自動合併?
3.如果運行了 git checkout new
,然後關閉git bash再打開git bash,預設的就是new分支了
4.git 預設的分支就是master
分支?
假如開發ubuntu,A程式設計師在開發LTS12.04
分支,然後B程式設計師卻繼續開發 V13
版本?
然後某一次B程式設計師需要去幫A程式設計師開發LTS12.04
,那B應該先將專案V13的所有檔案git push V13分支,再git checkout LTS12.04,再git pull拉取所有的LTS12. 04分支。修改後,再git push LTS12.04分支,再checkout V13分支,再 git pull V13所有程序,才返回了V13的分支?
是這樣嗎?但git pull不是合併了B專案資料夾下的V13和LTS12.04兩個版本的程式碼了嗎
難以理解…
語法錯誤。 。 。明確的報錯提示,都不看麼
push新分支應該這樣寫(你在哪裡看到是加冒號的 =。=)
===========================================
對分支的理解
建立新分支不代表切換到分支,
git branch new_branch
只是从当前分支的当前状态创建一个新的分支,但是此时直接修改并commit 还是在当前分支上的, 只有git checkout new_branch
才會切換到新建的分支git checkout -b new_branch
等效於上面兩個指令總和只有
push/pull/fetch
是跟远端交互的,commit/checkout
之类的都是纯本地操作checkout
只是從目前使用的分支切換到本地另外一個分支,換句話說就是把目前工作空間的所有檔案內容變成另一個分支的狀態"從遠端下載new分支到本地,直接自動合併" 是在new分支上
git pull
的效果是的,分支狀態是持久化的,所有資訊都保存在.git目錄下
是的
分支的用途
假如開發ubuntu,A程式設計師在開發LTS12.04分支,然後B程式設計師卻繼續開發 V13 版本?
然後某一次B程式設計師需要去幫A程式設計師開發LTS12.04,那B應該先將專案V13的所有檔案git push V13分支
不需要push,但必须commit/stash 否则修改内容会丢失
,再 git checkout LTS12.04,再git pull拉取所有的LTS12.04分支这里应该描述为拉取1204分支上所有的远端变更
。修改后,先git commit
再git push LTS12.04分支,再checkout V13分支,再 git pull V13所有程序同样不需要pull
,才回到了V13的分支?是這樣嗎?但git pull不是合併了B專案資料夾下的V13和LTS12.04兩個版本的程式碼了嗎
pull只会从远端拉取其他人push上去的变更,合并到本地“对应”的分支,跨分支的合并只能在本地通过git merge完成
Git 在呼喚你好好的看下人家的報錯訊息啊! 執行完
就會正常了,而且
push
的命令是啊!什麼時候成了
origin:new
了啊!應該是
push
这个过程出现了问题:git push origin new
Q: 建立了分支之後,再 git add . 就加入了,然後git commit -m ""就提交到了目前的分支?
A: 提交代碼到本地
Q: 如果運行了 git checkout new,那麼是不是會從遠端下載new分支到本地,直接自動合併?
A: 不會從遠端下載
new
分支到本地,除非显示的调用git pull origin
Q: 如果運行了
git checkout new
,然后关闭git bash再打开git bash
,預設的就是new分支了A: 對的
Q:
gi
t 默认的分支就是master
分支?A:
git
默认分支是master
分支推薦從頭學習一下:https://github.com/numbbbbb/progit-zh-pdf-epub-mobi
把 newBranch 分支 push 上去的話, 你要在 master 上合併 new 分支: git merge newBranch
整體步驟: