When you clone from a remote repository, git actually automatically corresponds to the local branch master
and the remote master
branch, and the default name for the remote repository is origin
.
To view information for a remote library, use git remote
:
$ git remoteorigin
Or, use git remote -v
the display for more detailed information:
$ git remote -vorigin [email protected]:michaelliao/learngit.git (fetch)origin [email protected]:michaelliao/learngit.git (push)
The above shows the addresses that can be crawled and pushed origin
. If you do not have push permissions, you cannot see the address of the push.
Push Branch
The push branch is the push of all local commits on that branch to the remote library. When pushing, specify the local branch so that git pushes the branch to the remote branch of the remote library:
$ git push origin master
If you want to push other branches, for example dev
, change to:
$ git push origin dev
However, it is not necessary to push the local branch to the remote, so which branches need to be pushed and which do not?
master
The branch is the main branch, so synchronize with the remote at all times;
dev
Branch is the development branch, all members of the team need to work on it, so also need to synchronize with the remote;
Bug branches are only used to fix bugs locally, there is no need to push remote, unless the boss wants to see how many bugs you have fixed every week;
Whether or not the feature branch is pushed to the remote depends on whether you are working with your little partner to develop it.
In short, is in git, branch can completely in the local hide to play, whether to push, depending on your mood!
Crawl Branch
When people collaborate, everyone master
dev
pushes their own changes to and from the branch.
Now, simulate a small partner that you can clone on another computer (note to add SSH key to GitHub) or another directory on the same computer:
$ git clone [email protected]:michaelliao/learngit.gitCloning into ‘learngit‘...remote: Counting objects: 46, done.remote: Compressing objects: 100% (26/26), done.remote: Total 46 (delta 16), reused 45 (delta 15)Receiving objects: 100% (46/46), 15.69 KiB | 6 KiB/s, done.Resolving deltas: 100% (16/16), done.
When your partner clones from the remote repository, by default, your small partner sees only the local master
branch. Don't believe you can use the git branch
command to see:
$ git branch* master
Now that your little partner is developing on the dev
branch, you have to create a origin
remote dev
branch to local, and he uses this command to create a local dev
branch:
$ git checkout -b dev origin/dev
Now, he can continue to dev
modify on the, and then, from moment to time, dev
branch push
to remote:
$ git commit -m "add /usr/bin/env"[dev 291bea8] add /usr/bin/env 1 file changed, 1 insertion(+)$ git push origin devCounting objects: 5, done.Delta compression using up to 4 threads.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 349 bytes, done.Total 3 (delta 0), reused 0 (delta 0)To [email protected]:michaelliao/learngit.git fc38031..291bea8 dev -> dev
Your partner has origin/dev
pushed his submission to the branch, and you happen to have modified the same file and tried to push it:
$ git add hello.py $ git commit -m "add coding: utf-8"[dev bd6ae48] add coding: utf-8 1 file changed, 1 insertion(+)$ git push origin devTo [email protected]:michaelliao/learngit.git ! [rejected] dev -> dev (non-fast-forward)error: failed to push some refs to ‘[email protected]:michaelliao/learngit.git‘hint: Updates were rejected because the tip of your current branch is behindhint: its remote counterpart. Merge the remote changes (e.g. ‘git pull‘)hint: before pushing again.hint: See the ‘Note about fast-forwards‘ in ‘git push --help‘ for details.
Push failed because the latest submission of your little partner conflicts with the submission you are trying to push, and the workaround is simple, Git has prompted us to first use the git pull
latest commit from origin/dev
scratch, then merge locally, resolve the conflict, and then push:
$ git pullremote: Counting objects: 5, done.remote: Compressing objects: 100% (2/2), done.remote: Total 3 (delta 0), reused 3 (delta 0)Unpacking objects: 100% (3/3), done.From github.com:michaelliao/learngit fc38031..291bea8 dev -> origin/devThere is no tracking information for the current branch.Please specify which branch you want to merge with.See git-pull(1) for details git pull <remote> <branch>If you wish to set tracking information for this branch you can do so with: git branch --set-upstream dev origin/<branch>
git pull
Also failed because there is a link to the remote branch that does not specify a local dev
branch origin/dev
, depending on the prompt, the setting dev
and origin/dev
the link:
$ git branch --set-upstream dev origin/devBranch dev set up to track remote branch dev from origin.
Pull again:
$ git pullAuto-merging hello.pyCONFLICT (content): Merge conflict in hello.pyAutomatic merge failed; fix conflicts and then commit the result.
This is git pull
successful, but there are conflicts in the merge, which need to be resolved manually, and the workaround is exactly the same as the resolution conflict in branch management. After resolving, submit, and then push:
$ git commit -m "merge & fix hello.py"[dev adca45d] merge & fix hello.py$ git push origin devCounting objects: 10, done.Delta compression using up to 4 threads.Compressing objects: 100% (5/5), done.Writing objects: 100% (6/6), 747 bytes, done.Total 6 (delta 0), reused 0 (delta 0)To [email protected]:michaelliao/learngit.git 291bea8..adca45d dev -> dev
As a result, the working mode of multi-person collaboration is usually this:
First, you can try to git push origin branch-name
push your own changes;
If the push fails, because the remote branch is newer than your local, you need git pull
to first try to merge;
If there is a conflict in the merge, resolve the conflict and submit it locally;
No conflict or conflict resolution, and then git push origin branch-name
push to Succeed!
If git pull
"No tracking information" is indicated, then the link relationship between the local branch and the remote branch is not created, using the command git branch --set-upstream branch-name origin/branch-name
.
This is a multi-person collaborative mode of work, once familiar with, it is very simple.
Summary
View Remote library information, use git remote -v
;
Local new branches are not visible to others if they are not pushed to the remote;
Push branches from local, use git push origin branch-name
, if push fails, first use git pull
crawl remote new commit;
Local and remote branches corresponding to the branch, use git checkout -b branch-name origin/branch-name
, local and remote branch names are best consistent;
Establishing the Association of local and remote branches, using git branch --set-upstream branch-name origin/branch-name
;
From the remote crawl branch, use git pull
, if there is a conflict, you must first handle the conflict.
Transfer from Http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ 0013760174128707b935b0be6fc4fc6ace66c4f15618f8d000
Git Multi-person assistance