➜ mygit git:(master) git co -b develop Switched to a new branch 'develop' ➜ mygit git:(develop) git br test ➜ mygit git:(develop) git br -av * develop 93cd003 git gui test master 93cd003 git gui test test 93cd003 git gui test remotes/origin/master 93cd003 git gui test ➜ mygit git:(develop) git push fatal: The current branch develop has no upstream branch. To push the current branch and set the remote as upstream, use
➜ mygit git:(develop) git push --set-upstream origin develop Total 0 (delta 0), reused 0 (delta 0) To github.com:sail-y/git_demo.git * [new branch] develop -> develop Branch develop set up to track remote branch develop from origin.
这个时候如果我们在另外一台机器想拉取这个远程分支怎么办呢?
1 2 3 4 5 6 7 8 9
➜ mygit2 git:(master) git checkout -b develop origin/develop Branch develop set up to track remote branch develop from origin. Switched to a new branch 'develop' ➜ mygit2 git:(develop) git br -av * develop 93cd003 git gui test master f7b8c54 [ahead 2] Merge branch 'master' of github.com:sail-y/git_demo remotes/origin/HEAD -> origin/master remotes/origin/develop 93cd003 git gui test remotes/origin/master 93cd003 git gui test
只需在checkout的时候最后面跟上远程分支的名称origin/develop。
另一种推送分支的方式
1 2 3 4 5 6 7
➜ mygit2 git:(develop) git co test Switched to branch 'test' ➜ mygit2 git:(test) git push -u origin test Total 0 (delta 0), reused 0 (delta 0) To github.com:sail-y/git_demo.git * [new branch] test -> test Branch testset up to track remote branch test from origin.
注意git push -u和git push --set-upstream的作用,是一样的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
➜ mygit git:(develop) git fetch From github.com:sail-y/git_demo * [new branch] test -> origin/test ➜ mygit git:(develop) git br -av * develop 93cd003 git gui test master 93cd003 git gui test remotes/origin/develop 93cd003 git gui test remotes/origin/master 93cd003 git gui test remotes/origin/test 93cd003 git gui test ➜ mygit git:(develop) git checkout --track origin/test Branch testset up to track remote branch test from origin. Switched to a new branch 'test' ➜ mygit git:(test) git br -av develop 93cd003 git gui test master 93cd003 git gui test * test 93cd003 git gui test remotes/origin/develop 93cd003 git gui test remotes/origin/master 93cd003 git gui test remotes/origin/test 93cd003 git gui test
这里用另外一种方式拉取远程分支,git checkout --track origin/test实际上就是git checkout -b test origin/test的一种特殊简写,为什么说特殊呢,因为它并没有指定本地分支的名称,而是直接使用了远程分支的名称。
➜ mygit git:(develop) git co test Switched to branch 'test' Your branch is up-to-date with 'origin/test'. ➜ mygit git:(test) cat .git/HEAD ref: refs/heads/test
➜ mygit2 git:(develop) git remote show origin * remote origin Fetch URL: git@github.com:sail-y/git_demo.git Push URL: git@github.com:sail-y/git_demo.git HEAD branch: master Remote branches: master tracked refs/remotes/origin/develop stale (use 'git remote prune' to remove) test tracked Local branches configured for'git pull': develop merges with remote develop master merges with remote master test merges with remote test Local refs configured for'git push': master pushes to master (fast-forwardable) test pushes to test (up to date)
注意这里是另外一个用户,这里git给了提示:refs/remotes/origin/develop stale (use ‘git remote prune’ to remove)