GIT branch Management and Conflict resolution (reprint)

Source: Internet
Author: User
Tags using git

Git Branch Management and conflict resolution original: Http://www.cnblogs.com/mengdd/p/3585038.html

Create a branch

Git branch

There are no parameters to display all local branch names in the local repository.

The current checkout branch is preceded by an asterisk.

Git branch newname

Creates a new branch on the currently checked out branch, named NewName.

git checkout newname

Check out the branch, that is, switch to the branch called NewName.

git checkout–b newname Master

This command merges the above two commands: Creates a branch NewName branch on the master branch and checks out to that branch.

Merging changes between branches merge

The merge operation merges two or more branches together, and there are actually several branch merge methods, the main three of which are described below:

  1. Direct merge (straight merge):

Merge the historical tracks of the two branches and converge them together.

For example, to merge all the things on the dev branch into the master branch:

First to Master branch:git checkout master

Then merge the dev:git merge dev

Note that the merge is fast-forward without parameters, that is, Git moves the pointer of the master branch directly to the front of Dev.

In other words, if you go down one branch and you reach another, git will simply move the pointer as it merges the two, so this merge becomes fast-forward (fast-forward).

  2. Press-Fit Merge (squashed commits):

Press a commit entry on a branch to submit to the tip of another branch.

Press all commits on the dev branch to synthesize a commit on the main branch, i.e. press-fit commit:

git checkout Master

git merge--squash Dev

At this point, all commits on Dev have been merged into the current workspace and staged, but have not yet been submitted as a commit to commit this change to the repository, as with other commits:

Git commit–m "something from Dev"

  

  3. Picking merge (cherry-picking):

Picking changes to a commit entry on another branch to the current branch.

Each commit produces a globally unique commit name that can be used for picking submissions.

For example, a commit on Dev is called: 321d76f

Merge it into master:

git checkout Master

Git Cherry-pick 321d76f

To pick multiple commits, you can pass the-n option to the git cherry-pick command, such as:

Git cherry-pick–n 321d76f

This allows for staging without immediate submission after selecting this change, and then the next pick operation, which can be submitted once the required submissions have been selected.

Conflict handling

When two branches make different changes to the same text block of the same file and try to merge, Git cannot merge automatically, called Conflict (conflict). Resolving conflicts requires manual processing.

For example, in the master branch, want to merge the dev branch, resulting in a conflict, open the file content can see a conflict:

<<<<<<< headtest in master=======test in Dev>>>>>>> Dev

  The <<<<<<< tag conflict begins with the contents of the current branch.

  The HEAD points to the commit of the current branch's distal.

  After the ======= ,>>>>>>> was the code on the other branch to merge over.

  Dev after >>>>>>> is the name of the branch.

  For a simple merge, edit it manually, then remove the tags, and finally add the commit as usual.

Delete Branch

Some branches have no need for long-term preservation, such as the code in the branch has been tagged and published, or the experimental branch has successfully completed the work or abandoned, and so on.

  Note : A labeled Branch, when git deletes the branch, all the historical traces from the beginning of the version tree to the tag are preserved, and the delete branch operation simply removes the name of the branch itself, so it is not necessary to save the branch for a long period of time.

In other versions of the control tool, deleting a branch usually means deleting all the historical traces on the branch, so it is not considered necessary to save it because it is labeled.

Delete a branch Dev2:

Git branch–d dev2

Note You cannot delete the current branch, and you need to go to another branch.

If the branch to be removed has been successfully merged into the current branch, the operation to delete the branch succeeds directly.

If the branch that you want to delete is not merged into the current branch, a prompt appears and if you decide that you want to delete it without merging, execute the command:

Git branch–d dev2

be strongly deleted.

Branch Rename

To rename a branch:

  git branch–m oldname newname

  - m does not overwrite the existing branch name, meaning that if a branch named NewName already exists, the hint already exists.

If you change to- m to overwrite an existing branch name, you will be forced to overwrite the branch named NewName, which is prudent.

Resources

Versioning-Using Git (pragmatic version control using GIT)

GIT branch management policy: http://www.ruanyifeng.com/blog/2012/07/git.html

Git reference:http://gitref.org/

GIT branch Management and Conflict resolution (reprint)

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.