Git remote operation

Source: Internet
Author: User
Tags using git

A trace branch and trace remote Branch

When it comes to git remote operations, there are two concepts that need to be mastered:
Trace Branch--tracking branch
Trace remote Branches--Remote tracking branch
So in git local operations, the branch is just a reference to a commit object, so what does the tracking branch and the remote trace branch specifically refer to in the local repository?

, Https://yourrepohost/project.git is a remote repository, at which point the remote master Branch points to version C2

The Git clone https://yourrepohost/project.git is a remote repository clone to local repo, which forms a origin/master reference, which origin/ Master is what we call the remote tracing branch, which is read-only by the user. It also generates a local master branch based on Origin/master, which also points to C2, which is the tracking branch, which is user writable.

So what does user-readable, user-writable mean? Let's take a look at the picture below

When adding a local commit C3, the local master will also move to C3, and when git push to the remote repository, the remote repository master will also be moved to C3, when git push will do one thing and he will move the local origin/master to C3, The local repository cannot operate on origin/master.

In fact, this user read-only there is another layer of explanation, in the GIT local operation, using git checkout master to switch to the master branch, in fact, the head reference to the master branch, if you use Git checkout origin/master , the head reference does not point to Origin/master, and in this state, if you do some commits and switch back to the master branch and then switch back to Origin/master, the last committed file will not exist unless you use Reflog ( Maintain the change history of the head reference) to find it back.

Main commands for two-git remote collaboration

In addition to git clone is the first time to get data from the server, we can also get data from the server via git fetch and git pull. Git fetch is equivalent to getting the latest version from remote to local, but not automatically merge, and Git pull is equivalent to getting the latest version from remote and merge to local, the git pull command is equivalent to git fetch and git merge. In practice, git fetch is more secure, because before the merge, we can look at the updates and then decide whether to merge.

Here are a few scenarios:

  Scenario 1: Use git clone to clone the remote Master branch on GitHub to local, then modify the files on the remote master branch on GitHub, and if you want to get the content from the remote Master branch locally, you can use git fetch to pull data

git fetch to see the historical information, you can see, at this time Origin/master has moved to the latest version, and the local master/head is still in the original location

Then use git merge origin/master to merge Origin/master so that the local master/head also moves to the latest version

  

  Scenario 2: Suppose I modified the Remote.txt file locally, and another person made a change to remote.txt and git push it to the remote branch, I can't do it locally if I use git push. Since there are some files on the remote branch that you do not have locally, if you force push, you will overwrite the files on the remote branch, so in this case you must first git pull (git fetch+git merge) to get the latest data from the remote to the local

However, you can see that both local and remote have modified the same file, resulting in a conflict, where the merge will prompt you to handle the conflict.

The content between <<<<<<< and ===== is a local modification, and the content between ===== and >>>>>> is a modification made by the remote branch.

deleting unwanted content and then making git add-git commit-git push can push the content to the remote repository.

If you want to tag the latest commit after git push, you can use git tag-a v1-m "tag for v1" To send the annotated tag,git push to this submission is by default not to push the tag to the remote branch, so use git push--tag Tag the version on the remote branch

  Scenario 3: Suppose that both the local and the remote warehouses have a feacher branch, and we want to delete what to do?

Switch to a branch other than the Feacher branch, such as the Master branch, and then use Git branch-d feacher to first delete the local Feacher branch and then use git push--delete origin Feacher removed Feacher branch from remote repository

There is another way to delete a branch in the remote repository, using git push origin:feacher to remove it, which means using an empty branch in the remote repository instead of the Feacher branch, notice that Origin is preceded by a space and followed by a colon

  

  

Git remote operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.