- When you clone from a remote repository, Git actually automatically puts the local
master
Branch and remotemaster
The branches correspond, and the default name of the remote repository isorigin
. - To view information about remote libraries, use the
git remote
:
$ git remote
origin
Copy the code
- Or, in
git remote -v
Display 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 pushed
origin
The address. If you don’t have push permission, you can’t see itpush
The 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?
master
The branch is the primary branch and therefore synchronizes with the remote at all times;dev
A 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.
feature
Whether 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 library
clone
By default, your friends can only see localmaster
Branch. You can use itgit branch
Command to see:
$ git branch
* master
Copy the code
- Now, your little friend is gonna be in
dev
To develop on a branch, you must create a remoteorigin
thedev
Branch 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 there
dev
And then, from time to timedev
branchpush
To 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 to
origin/dev
The 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 first
git pull
Take the latest submission fromorigin/dev
Grab 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 pull
Also failed because no local was specifieddev
Branch and Remoteorigin/dev
Branch links, as prompted, setdev
andorigin/dev
The link:
$ git branch --set-upstream-to=origin/dev dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
Copy the code
- again
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.
Copy the code
- This time,
git pull
Yes, 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:
- First of all, you can try to use
git push origin <branch-name>
Push your own changes; - If the push fails, the remote branch needs to be used first because it is newer than your local branch
git pull
Attempted merger; - If the merge has conflicts, resolve the conflicts and commit locally;
- Use only when there is no conflict or conflict is resolved
git 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, use
git remote -v
; - Branches created locally are not visible to others unless they are pushed remotely.
- Push branch from local, using
git push origin branch-name
If push fails, grab the new remote commit with Git pull first. - To create a local branch that corresponds to a remote branch, use
git checkout -b branch-name origin/branch-name
, local and remote branch names should be the same; - To establish associations between local and remote branches, use
git branch --set-upstream branch-name origin/branch-name
; - Fetching branches from remote, using
git pull
If there are conflicts, deal with them first.