Git branch remote connection and remote branch pull push and conflict handling



This article refers to teacher Liao Xuefeng’s blogGit tutorial。 Learn and record according to his blog. Thank him for his selfless sharing. You are also welcome to check the original text.

Knowledge points

  • Force the deletion of unconsolidated branches,git branch -D <branch-name>, the modification of the branch will be lost
  • git remoteandgit remote -v Display remote warehouse information
  • git push origin branch-namePush local branch to remote.
  • After the default clone remote library, you can only see the master branch, and other branches need to set tracking,git checkout -b dev origin/devDev branch set to track fromoriginRemote branch ofdev
  • git branch --set-upstream-to=origin/<branch> branchCreate a trace of local and remote branches forgit pushandgit pull
  • git pull <remote> <branch>Pull the specified remote branch

Feature branch

In software development, there are always endless new functions to be added.

When adding a new function, you certainly don’t want to mess up the main branch because of some experimental code. Therefore, it’s best to create a new function every time you add a new functionfeatureBranch, develop above, merge after completion, and finally delete thefeatureBranch.

For example, you have received a new task: the development code isVulcanThe new feature is planned for the next generation of starships.

So we are ready to develop:

$ git checkout -b feature-vulcan
Switched to a new branch 'feature-vulcan'

After 5 minutes, the development is completed:

$ git add
$ git status
# On branch feature-vulcan
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#       new file:
$ git commit -m "add feature vulcan"
[feature-vulcan 756d4af] add feature vulcan
 1 file changed, 2 insertions(+)
 create mode 100644

Cut backdev, preparing to merge:

$ git checkout dev

If everything goes well,featureBranching andbugBranches are similar, merge, and then delete.

However, this function needs to be cancelled for some reasons

That is, this branch needs to be destroyed locally:

$ git branch -d feature-vulcan
error: The branch 'feature-vulcan' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature-vulcan'.

Destroy failed. Git tip,feature-vulcanThe branches have not been merged yet. If they are deleted, the modifications will be lost. If they are to be forcibly deleted, you need to use the commandgit branch -D feature-vulcan

Now let’s forcibly delete:

$ git branch -D feature-vulcan
Deleted branch feature-vulcan (was 756d4af).

Multi person collaboration

  • Clone from remote warehouse, GIT automaticallymasterBranch and remotemasterCorresponding to the branch, the default name of the remote warehouse isorigin
  • View remote warehouse information,git remote
$ git remote
  • git remote -v Show more details of remote warehouse
$ git remote -v
origin    [email protected]:findmoon/newrepo.git (fetch)
origin    [email protected]:findmoon/newrepo.git (push)

The above is for pulling and pushingoriginAddress, with two permissions of pull and push

Push branch

  • Push branch is to push all local submissions on the branch to the remote library. When pushing, you need to specify a local branch. Git will push the branch to the corresponding remote branch:
$ git push origin master

The above will push the local master to the origin master. Other local branches will not be pushed

  • git push origin dev, push other branches. If there are no remote branches, create them(dev)Branch and push

Remote branch push recommendation

  • masterThe branch is the primary branch, so it should be synchronized with the remote branch at all times
  • devThe branch is a development branch. All team members need to work on it, so they also need to synchronize with the remote
  • bugThe branch is only used to fix bugs locally, so there is no need to push it to the remote, unless the boss wants to see how many bugs you fix every week
  • featureWhether the branch is pushed to the remote depends on whether you develop it in cooperation with your small partners

Clone remote warehouse

In multi person collaboration, for example, simulate another small partner (another computer or another directory) to clone the warehouse remotely

$ git clone
Cloning to 'newrepo'
remote: Counting objects: 55, done.
remote: Compressing objects: 100% (33/33), done.
remote: Total 55 (delta 20), reused 54 (delta 19), pack-reused 0
Expand objects: 100% (55 / 55), complete
Check connection Done.

Branch push and conflict handling

Associate local and remote branches

  • By default, from a remote libraryclone, only remotemasterBranch localmasterbranch
$ cd newrepo/
$ git branch
* master
  • If you want todevFor development on branches, you need to create localdevBranch and set to track remoteoriginofdevbranch
$ git checkout -b dev origin/dev
Branch dev is set to track remote branch dev from origin.
Switch to a new branch 'dev'
  • The new branch has been synchronized with the remote warehouse.

Now another partner modifiesdevBranch andpushTo remote

$ git push origin dev
Username for '': findmoon
Password for 'https://[email protected]':
Object count: 3, complete
Delta compression using up to 4 threads.
Compressed objects: 100% (3 / 3), complete
Write to object: 100% (3 / 3), 319 bytes | 0 bytes / s, complete
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
   09a36ec..5a15ca7  dev -> dev

The new local warehouse is pushed to the remote warehouse for the first time. Input requiredgithubUser name and password for

Specify branch or set Branch tracking during push

Push in small partnerorigin/devAfter that, you also modified the same file and pushed it

$ git push origin dev
To [email protected]:findmoon/newrepo.git
 ! [rejected]        dev -> dev (fetch first)
Error: unable to push some references to ' [email protected] :findmoon/newrepo. git'
Tip: the update was rejected because the remote warehouse contains a submission that does not exist locally. This is usually because of another reason
Tip: a warehouse has pushed the reference. You may need to integrate remote changes before pushing again
Prompt: (such as' git pull... ').
Note: see the 'note about fast forwards' section in' git push -- help 'for details.

Prompt: unable to push, update rejected, GIT prompt: the push needs to integrate the changes first

  • When there are changes in the remote warehouse, you need to integrate the changes and use them againgit pull

usegit pullPull the latest remote submission

$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 2), reused 3 (delta 2), pack-reused 0
Expand objects: 100% (3 / 3), complete
From GitHub com:findmoon/newrepo
   09a36ec..5a15ca7  dev        -> origin/dev
The current branch has no tracking information.
Please specify which branch you want to merge.
See git pull (1) for details.
    git pull <remote> <branch>
If you want to create trace information for this branch, you can:
    git branch --set-upstream-to=origin/<branch> dev

git pullFailed because:git pullLocal branch and remote branch need to be specifiedoriginBranch tracking, orgit pullSpecify the remote branch in the parameter

  • git branch --set-upstream-to=origin/<branch> branchCreate a trace of local and remote branches forgit pushandgit pull
  • git pull <remote> <branch>Specifies the remote branch to pull

Set trace remote branch

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

File conflict when pulling branch

After the tracking is set up above, restartpullPull update

$ git branch --set-upstream-to=origin/dev dev
Branch dev is set to track remote branch dev from origin.
$ git pull
Auto merge readme txt
Conflict (content): merge conflicts with readme txt
Auto merge failed. Correct the conflict and submit the correction result.

Now you cangit pull, but the merger conflicts.Conflict resolution is the same as in local branch management

  • Manual modificationgit pullMerge conflicts, then commit, and finallypush

View conflict files

$ cat readme.txt
dev modify again commit on master
<<<<<<< HEAD
I modify readme on dev branch
modify dev branch file on another people
>>>>>>> 5a15ca7c06dd2204fee3f4571e86b2bf64f6a83b

Add, submit, and push again after deleting the conflict item.

$ git add .
$ git commit -m"fixed remote conflict"
[dev 8a954f4] fixed remote conflict
$ git push origin dev
Object count: 6, complete
Delta compression using up to 4 threads.
Compressed objects: 100% (6 / 6), complete
Write to object: 100% (6 / 6), 570 bytes | 0 bytes / s, complete
Total 6 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 2 local objects.
To [email protected]:findmoon/newrepo.git
   5a15ca7..8a954f4  dev -> dev

Pull and push are completed.

Another little partner directlygit pull, keep updated with the remote library, and the remote library content will be merged locally.

$ git pull origin dev
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 4), reused 6 (delta 4), pack-reused 0
Expanded objects: 100% (6 / 6), complete
come from
 * branch            dev        -> FETCH_HEAD
   5a15ca7..8a954f4  dev        -> origin/dev
Update 5a15ca7 8a954f4
 readme.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Suggestions on working mode of multi person cooperation

  • First, you can try to usegit push origin branch-namePush your own modifications
  • If the push fails, the remote branch needs to be used first because it is newer than your local branchgit pullAttempt to merge
  • If there is a conflict in the merge, resolve the conflict and submit locally
  • No conflict or after resolving the conflict, use it againgit push origin branch-namePush will succeed

Recommended Today

Simple principles of web page layout design

This paper sorts out some simple principles of web page layout design, hoping to be useful to web page designers.・ repeat: repeat some page design styles throughout the site. The repeated components may be a certain font, title logo, navigation menu, blank edge setting of the page, lines running through the specific thickness of the […]