• When you clone from a remote repository, Git actually automatically puts the localmasterBranch and remotemasterThe branches correspond, and the default name of the remote repository isorigin.
  • To view information about remote libraries, use thegit remote:
$ git remote
origin
Copy the code
  • Or, ingit remote -vDisplay more detailed information:
$git remote -v origin [email protected]: user name /learngit.git (fetch) origin [email protected]: user name /learngit.git (push)Copy the code
  • It shows what can be grabbed and pushedoriginThe address. If you don’t have push permission, you can’t see itpushThe address.

Push the branch

  • A push branch pushes all local commits on that branch to a remote repository. Git will push this branch to the corresponding remote branch of the remote library:
$ git push origin master
Copy the code
  • If you want to push another branch, such as dev, change it to:
$ git push origin dev
Copy the code
  • However, it is not necessary to push local branches to remote locations, so which branches need to be pushed and which do not?
  • masterThe branch is the primary branch and therefore synchronizes with the remote at all times;
  • devA branch is a development branch where all team members need to work, so it also needs remote synchronization;
  • The bug branch is only used to fix bugs locally, so there’s no need to push it remotely unless your boss wants to see how many bugs you fix per week.
  • featureWhether the branch is pushed to a remote location depends on whether or not you work with your friends on it.

All in all, in Git, branches can be played locally, and pushed or not, depending on your mood!

Fetching branches

  • When a group collaborates, everyone pushes their changes to the Master and Dev branches.
  • Now, to simulate a friend, clone it on another computer (make sure you add SSH keys to GitHub) or in another directory on the same computer:
$ git clone [email protected]: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.
Copy the code
  • When your buddy from the remote librarycloneBy default, your friends can only see localmasterBranch. You can use itgit branchCommand to see:
$ git branch
* master
Copy the code
  • Now, your little friend is gonna be indevTo develop on a branch, you must create a remoteoriginthedevBranch to local, so he creates a local dev branch with this command:
$ git checkout -b dev origin/dev
Copy the code
  • Right now, he can be theredevAnd then, from time to timedevbranchpushTo 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 -> devCopy the code
  • Your little friend has been toorigin/devThe branch pushes his commit, and you happen to make changes to the same file and try 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.
Copy the code
  • Because your buddy’s latest submission conflicts with the submission you are trying to push. The solution is also very simple. Git has reminded us to use it firstgit pullTake the latest submission fromorigin/devGrab it, then merge it locally, resolve the conflict, and push it again:
$ 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
Copy the code
  • git pullAlso failed because no local was specifieddevBranch and Remoteorigin/devBranch links, as prompted, setdevandorigin/devThe link:
$ git branch --set-upstream-to=origin/dev dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
Copy the code
  • againpull:
$ git pull
Auto-merging env.txt
CONFLICT (add/add): Merge conflict in env.txt
Automatic merge failed; fix conflicts and then commit the result.
Copy the code
  • This time,git pullYes, but the merge has conflicts that need to be resolved manually in the same way that conflicts are resolved in branch management. After settlement, submit againpush:
$ 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 -> devCopy the code
  • Therefore, the working mode of multi-person collaboration usually looks like this:
  1. First of all, you can try to usegit push origin <branch-name>Push your own changes;
  2. If the push fails, the remote branch needs to be used first because it is newer than your local branchgit pullAttempted merger;
  3. If the merge has conflicts, resolve the conflicts and commit locally;
  4. Use only when there is no conflict or conflict is resolvedgit push origin <branch-name>Push can succeed!

If git pull prompts no tracking information, the link between the local and remote branches has not been created. Git branch –set-upstream to

origin/

This is how people work together, and once you get used to it, it’s pretty easy.

summary

  • To view remote library information, usegit remote -v;
  • Branches created locally are not visible to others unless they are pushed remotely.
  • Push branch from local, usinggit push origin branch-nameIf push fails, grab the new remote commit with Git pull first.
  • To create a local branch that corresponds to a remote branch, usegit checkout -b branch-name origin/branch-name, local and remote branch names should be the same;
  • To establish associations between local and remote branches, usegit branch --set-upstream branch-name origin/branch-name;
  • Fetching branches from remote, usinggit pullIf there are conflicts, deal with them first.