first , We created dev branch , Then switch to dev branch ：
$ git checkout -b dev Switched to a new branch 'dev'
git checkout Order plus -b Parameter means create and switch , It is equivalent to the following two commands ：
$ git branch dev $ git checkout dev Switched to branch 'dev'
then , use git branch Command to view the current branch ：
$ git branch * dev master
git branch The command lists all branches , The current branch will be preceded by one * Number .
then , We can be there dev Normal commit on branch , For example, yes readme.txt Make a change , Add a line ：
Creating a new branch is quick.
Then submit ：
$ git add readme.txt $ git commit -m "branch test" [dev b17d20e] branch test 1
file changed, 1 insertion(+)
Now? ,dev Branch work completed , We can switch back master branch ：
$ git checkout master Switched to branch 'master'
Switch back to master After branching , Check out another one readme.txt file , The content just added is missing ! Because the submission was made in dev On the branch , and master The commit point of the branch is not changed at the moment ：
Now? , We put dev The branch's work products are merged into master On the branch ：
$ git merge dev Updating d46f35e..b17d20e Fast-forward readme.txt | 1 + 1 file
changed, 1 insertion(+)
git merge The command merges the specified branch to the current branch . After merger , Check again readme.txt Content of , You can see that , and dev The latest commit of the branch is exactly the same .
Local branch conflict
Prepare new feature1 branch , Continue with our new branch development ：
$ git switch -c feature1 Switched to a new branch 'feature1'
modify readme.txt Last line , Change to ：
Creating a new branch is quick AND simple.
stay feature1 Commit on branch ：
$ git add readme.txt $ git commit -m "AND simple" [feature1 14096d0] AND
simple 1 file changed, 1 insertion(+), 1 deletion(-)
Switch to master branch ：
$ git switch master Switched to branch 'master' Your branch is ahead of
'origin/master' by 1 commit. (use "git push" to publish your local commits)
Git It will also automatically prompt us about the current situation master Branching is more remote master Branch ahead 1 Submissions .
stay master On the branch readme.txt Change the last line of the file to ：
Creating a new branch is quick & simple.
$ git add readme.txt $ git commit -m "& simple" [master 5dc6824] & simple 1
file changed, 1 insertion(+), 1 deletion(-)
Now? ,master Branching and feature1 Each branch has a new commit , That's what happened ：
In this case ,Git Unable to execute “ Quick merge ”, Only attempt to merge the respective changes , But there may be conflicts , Let's try ：
$ git merge feature1 Auto-merging readme.txt CONFLICT (content): Merge
conflict in readme.txt Automatic merge failed; fix conflicts and then commit
Git Tell us ,readme.txt There is a conflict in the file , You must resolve the conflict manually before committing .git status You can also tell us about conflicting documents ：
$ git status On branch master Your branch is ahead of 'origin/master' by 2
commits. (use "git push" to publish your local commits) You have unmerged
paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort
the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both
modified: readme.txt no changes added to commit (use "git add" and/or "git
We can check it directly readme.txt Content of ：
Git is a distributed version control system. Git is free software distributed
under the GPL. Git has a mutable index called stage. Git tracks changes of
files. <<<<<<< HEAD Creating a new branch is quick & simple. ======= Creating a
new branch is quick AND simple. >>>>>>> feature1
Git use <<<<<<<,=======,>>>>>>> Mark the contents of different branches , We modify it as follows and save it ：
Creating a new branch is quick and simple.
Re submit ：
$ git add readme.txt $ git commit -m "conflict fixed" [master cf810e4]
When you clone from a remote repository , actually Git Automatically put the local master Branching and remote master The branches correspond , also , The default name of the remote warehouse is origin
. To view information about a remote library , use git remote：
$ git remote origin
perhaps , use git remote -v Show more details ：
$ git remote -v origin firstname.lastname@example.org:yourname/learngit.git (fetch) origin
It shows what you can grab and push origin Address of . If you do not have push permission , You can't see it push Address of .
Push branch , That is to push all local commits on the branch to the remote library . When pushing , To specify a local branch , such ,Git The branch is pushed to the corresponding remote branch of the remote library ：
$ git push origin master
If you want to push other branches , such as dev, It's changed to ：
$ git push origin dev
however , It is not necessary to push local branches to remote locations , that , Which branches need to be pushed , What don't you need ?
master The branch is the main branch , So always synchronize with the remote ;
dev A branch is a development branch , All members of the team need to work on it , So it also needs to be synchronized with remote ;
bug Branches are only used for local repair bug, There's no need to push it remotely , Unless the boss wants to see how many repairs you fix each week bug;
feature Whether branch is pushed to remote , It depends on whether you work with your partner to develop it .
Multi person branch conflict
This is usually the working mode of multi person collaboration ：
first , You can try to use git push origin <branch-name> Push your own changes ;
If the push fails , Because the remote branch is newer than your local branch , You need to use it first git pull Attempt to merge ;
If there is a conflict in the merger , Then resolve the conflict , And submit it locally ;
There is no conflict or after the conflict is resolved , Reuse git push origin <branch-name> Push will succeed !
If git pull Tips no tracking information, The link relationship between the local branch and the remote branch is not created , Use the command git branch
--set-upstream-to <branch-name> origin/<branch-name>.
actual combat ：
If your partner has origin/dev The branch pushed his submission , And you happen to have made changes to the same document , And try to push ：
$ git push origin dev To github.com:yourname/learngit.git ! [rejected] dev ->
dev (non-fast-forward) error: failed to push some refs to
'email@example.com:yourname/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 your partner's latest submission conflicts with the submission you are trying to push , The solution is simple, too ,Git We've been prompted , First use git pull Put the latest Submission from origin/dev
Catch it , then , Merge locally , Conflict resolution , Push 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
git pull And failed , The reason is that local is not specified dev Branch and remote origin/dev Links to branches , According to the prompt , set up dev and origin/dev Link to ：
$ git branch --set-upstream-to=origin/dev dev Branch 'dev' set up to track
remote branch 'dev' from 'origin'.
$ git pull Auto-merging env.txt CONFLICT (add/add): Merge conflict in env.txt
Automatic merge failed; fix conflicts and then commit the result.