git study notes - many people work together

When you clone from a remote repository, in fact, Git automatically local masterbranch and a remote masterbranch of the association, and the default name of the remote warehouse is origin.

To view information about a remote repository, use git remote:

$ git remote
origin

Alternatively, with git remote -vthe display more detailed information:

$ git remote -v
origin  git@github.com:michaelliao/learngit.git (fetch)
origin  git@github.com:michaelliao/learngit.git (push) 

Shown above can grab and push originaddress. If you do not push access, you can not see the push address.

Push branch

Push branch, that is, all the local branch of the submission pushed to the remote repository. When pushed, to specify the local branch, so that, Git will branch to the branch pushed to the remote database on the remote corresponding to:

$ git push origin master

If you want to push other branches, for example dev, to read:

$ git push origin dev

However, not must take the local branch to remote push, then what needs to push branches, which do not need it?

  • masterBranches are the main branch, so to be synchronized with a remote time;

  • devBranch is the development branch, all team members need to work on it, so it needs to be synchronized with the remote;

  • branch bug fix for the bug only locally, there is no need to push the remote, unless the boss wants to see you every week in the end several bug fixes;

  • feature branch is expected to push the remote, depending on whether or not you and your little partner to develop on it.

In short, is in Git, branching can own hidden play locally, whether push, depending on your mood may be!

 

Crawl branch

When many people work together, we will go masterand devthe branches push their own changes.

Now, you simulate a small partner, it can be another computer (SSH Key note should be added to GitHub) or the same directory another computer clone:

$ git clone git@github.com:michaelliao/learngit.git
Cloning into 'learngit'... remote: Counting objects: 40, done. remote: Compressing objects: 100% (21/21), done. remote: Total 40 (delta 14), reused 40 (delta 14), pack-reused 0 Receiving objects: 100% (40/40), done. Resolving deltas: 100% (14/14), done. 

When your little clone libraries from remote partner, by default, you can only see a small partner of the local masterbranch. Do not believe you can use git branchthe command to see:

$ git branch
* master

Now, to your little partner in devthe development of the branch, it is necessary to create the remote origin's devbranches to local, so he used this command to create a local devbranch:

$ git checkout -b dev origin/dev

Now, he can devcontinue to modify, and then, from time to time to devbranch pushto the remote:

$ git add env.txt

$ git commit -m "add env"
[dev 7a5e5dd] add env 1 file changed, 1 insertion(+) create mode 100644 env.txt $ git push origin dev Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 308 bytes | 308.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To github.com:michaelliao/learngit.git f52c633..7a5e5dd dev -> dev

Your partner has little to origin/devpush his submission branch, and you happen to be on the same file has been modified, and tried to push:

$ cat env.txt
env

$ git add env.txt

$ git commit -m "add new env"
[dev 7bd91f1] add new env 1 file changed, 1 insertion(+) create mode 100644 env.txt $ git push origin dev To github.com: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 behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. 

Push failed because the latest submitted your little partner and you try to submit the push of a conflict, the solution is simple, Git has prompted us to use git pullthe latest submission from origin/devdown catch, and then, in the local merger, conflict resolution, and push them up:

$ git pull
There 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-to=origin/<branch> dev 

git pullAlso failed, because no local devbranch and remote origin/devlinked branching, prompted, set up devand origin/devlink:

$ git branch --set-upstream-to=origin/dev dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'. 

Then pull:

$ git pull
Auto-merging env.txt
CONFLICT (add/add): Merge conflict in env.txt Automatic merge failed; fix conflicts and then commit the result. 

This time git pullsuccess, but associated with the conflict, need to manually solve, solving methods and branch management to resolve the conflict exactly the same. After resolving to submit, then push:

$ git commit -m "fix env conflict"
[dev 57c53ab] fix env conflict

$ git push origin dev
Counting objects: 6, done. Delta compression using up to 4 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 621 bytes | 621.00 KiB/s, done. Total 6 (delta 0), reused 0 (delta 0) To github.com:michaelliao/learngit.git 7a5e5dd..57c53ab dev -> dev 

 

Therefore, collaborative multiplayer mode of operation is usually like this:

  1. First, you can try to use git push origin <branch-name>to push their modification;

  2. If the push fails, because the remote is newer than your local branch, we need to use git pulltried to merge;

  3. If the merger there is a conflict, the resolution of the conflict, and commit locally;

  4. There is no conflict or post-conflict rid of, and then git push origin <branch-name>push will succeed!

If git pullprompted no tracking information, then the local branch and remote branch relationship did not create a link, use the command git branch --set-upstream-to <branch-name> origin/<branch-name>.

This is the multiplayer mode of collaborative work, once familiar, very simple.

summary

  • View remote database information, use git remote -v;

  • The new local branch if you do not push the remote, that is not visible to others;

  • Push from the local branch, use git push origin branch-name, if the push fails, first with git pullnewly submitted grab the remote;

  • Creating local and remote branch corresponding branch, using git checkout -b branch-name origin/branch-namethe name of the best local and remote branch agreement;

  • Associate local branch and a remote branch using git branch --set-upstream branch-name origin/branch-name;

  • Grab from a remote branch, use git pull, if there is a conflict, we must first deal with conflict.

Guess you like

Origin www.cnblogs.com/saryli/p/11369173.html