Git-branch management

Source: Internet
Author: User
Tags git mergetool

Git-branch management

 

Software Version:
Operating System: ubuntu10.04
Kernel version: Linux version 2.6.32-36-generic
Git version: git version 1.7.0.4

Directory:

1. Introduction
2. Create a branch
3. Switch Branch
4. merge branches
5. Delete Branch
6. branch management operations
6.1 branch list
6.2 view merged branches
6.3 view unmerged branches
6.4 rename a branch
7. Branch derivation (rebase)
8. References

1. Introduction

In essence, a branch is a variable pointer pointing to a certain commit. The default branch name of git is master. But how do we know which branch is currently in? The answer is that head is a very special pointer dedicated to pointing to the current branch in the local branch. We can understand it as follows:

commit <-- branch <-- HEAD

2. Create a branch

When we need to debug a bug or try to add or modify a module in the program, but it does not affect the development of the main branch. You can create a branch to meet your requirements.

Creating a branch is equivalent to creating a new branch pointer pointing to the current submission. For example, we create branch_1 on commit_3:

$git branch Branch_1

, Branch_1 points to submitting commit_3.

We can see that although we have created a new branch, the head still points to the master. If you want to switch to the new branch when creating the branch, run the following command:

$git checkout -b Branch_1

Head points to branch_1.

3. Switch Branch

In the process of project development, switching between branches is very common. For example, when debugging a bug on a branch, you suddenly need to add some code to the main branch, and you need to switch between the branches.

The switch between branches is to direct the head to the specified branch, and subsequent submissions will belong to the branch. Switch command:

$git checkout Branch_1

In this case, the submission belongs to the branch_1 branch.

4. merge branches

When the work on a branch is completed, it needs to be merged to another branch. Branch merge command:

$git merge branch_name

Now we want to perform a merge branch demonstration for the following projects:

Merge branch_1 to the master Branch:

$git merge branch_1 
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.

$git status
# On branch master
# Unmerged paths:
# (use "git add/rm <file>..." as appropriate to mark resolution)
#
# both modified: file
#
no changes added to commit (use "git add" and/or "git commit -a")

Since both commit_3 and B _c_1 have modified the same file, conflicts will occur during the merge process and need to be resolved manually. Unmerged paths in git status: This column shows which files are conflicted during the merge process.

Git adds tags to conflicting files, and the file content changes:

commit_1

<<<<<<< HEAD
commit_2
=======
branch_1 commit 1
>>>>>>> branch_1

We can see that some of the content of the file is separated by ======. The upper part is the content of the head, and the lower part is the content of branch_1. You can directly modify the file to solve the conflict, or use the GIT mergetool command to solve the problem.

We can use git mergetool to solve this problem.

$git mergetool 
merge tool candidates: meld opendiff kdiff3 tkdiff xxdiff tortoisemerge gvimdiff diffuse ecmerge p4merge araxis emerge vimdiff
Merging the files: file

Normal merge conflict for 'file':
{local}: modified
{remote}: modified
Hit return to start merge resolution tool (meld):

In my environment, meld is used by default to solve merge conflicts. Let's take a look at the GIT status after the solution is completed:

$git status
# On branch master
# Changes to be committed:
#
# modified: file
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# file.orig

The GIT status shows that the file has been put into the temporary storage zone. You only need to submit the file. File. orig is a backup file.

$git commit 
[master 3f8596e] Merge branch 'branch_1'
$git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# file.orig
nothing added to commit but untracked files present (use "git add" to track)

After submission, let's take a look at the submission log.

$git lg
* 3f8596e00cbd01320a31c0e7e2715ac12dffc48c @ (HEAD, master)
|\ Author: Eddy | | Date: Thu Mar 29 11:02:22 2012 +0800 (15 seconds ago)
| |
| | Commit subject: Merge branch 'branch_1'
| |
| * 6d5867c77debde72d12f3187ab972944f88624be @ (branch_1)
| | Author: Eddy | | Date: Thu Mar 29 10:25:50 2012 +0800 (37 minutes ago)
| |
| | Commit subject: branch_1: commit 1
| |
* | c773ca7c9a0154876b7de4ed66cabe704da6f29d @
|/ Author: Eddy | Date: Tue Mar 27 08:58:52 2012 +0800 (2 days ago)
|
| Commit subject: commit_2
|
* c438c48d1cd9615490d8be830683487de858e7b3 @
Author: Eddy Date: Tue Mar 27 08:51:54 2012 +0800 (2 days ago)

Commit subject: commit_1

After the merge statement is submitted, branch_1 still points to the original commit. :

5. Delete Branch

The merged branch naturally loses its meaning. Now let's Delete the branch_1 branch.

$git branch -d branch_1 
Deleted branch branch_1 (was 6d5867c).

6. branch management operations

The following describes some common operations in branch management.

6.1 branch list

$ git branch
branch_1
branch_2
* master

The branch with the * sign indicates the current branch.

6.2 view merged branches

$ git branch --merged
branch_1
* master

6.3 view unmerged branches

$ git branch --no-merged
branch_2

6.4 rename a branch

$git branch -m branch_2 branch_renamed

7. Branch derivation (rebase)

A rebase patch is used to patch the current branch to the specified branch. The current branch is combined with the specified branch. The difference between it and merge is that the current branch will be merged with the specified Branch and a new commit will be generated, making the entire git Project look more neat; merge retains all branch submission records.
Let's assume that we have derived the GIT project shown in.

The following result is returned.

Let's demonstrate the specific operation process. Suppose we are in the branch_1 branch. Now we need to perform derivation with the master branch.

$git rebase master 
First, rewinding head to replay your work on top of it...
Applying: branch_1
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging file
CONFLICT (content): Merge conflict in file
Failed to merge in the changes.
Patch failed at 0001 branch_1

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

The file will be merged during the process of merging. From the above print, we can see that there is a conflict in the process of merging, resulting in the failure of the merging. Use the method mentioned in section 4 to resolve the conflict, and then continue the derivation according to the preceding printed message.

$git mergetool 
merge tool candidates: meld opendiff kdiff3 tkdiff xxdiff tortoisemerge gvimdiff diffuse ecmerge p4merge araxis emerge vimdiff
Merging the files: file

Normal merge conflict for 'file':
{local}: modified
{remote}: modified
Hit return to start merge resolution tool (meld):

$git rebase --continue
Applying: branch_1

After the derivation is completed, it is currently in the branch_1 branch ,:

Then we need to return to the master branch and merge the master and branch_1.

$git checkout master 
Switched to branch 'master'

$git merge branch_1
Updating 12c963d..82d3623
Fast-forward
file | 2 ++
1 files changed, 2 insertions(+), 0 deletions(-)

The final result is what we want.

$git status
# On branch master
nothing to commit (working directory clean)

$git lg
* 82d36231ff46f362885a8a369cb80cf95009058a @ (HEAD, master, branch_1)
| Author: Eddy | Date: Thu Mar 29 17:48:58 2012 +0800 (37 seconds ago)
|
| Commit subject: branch_1
|
* 12c963d83d23a7ffc78ca2e13935b5d36fbf5d76 @
| Author: Eddy | Date: Thu Mar 29 16:22:39 2012 +0800 (87 minutes ago)
|
| Commit subject: commit 3
|
* 0850a6759874ed23042cb0fddb43b5c7d28e5b7b @
| Author: Eddy | Date: Thu Mar 29 16:21:19 2012 +0800 (88 minutes ago)
|
| Commit subject: commit 2
|
* 82e24403d7b4a562dedf343955df8196fe353bf6 @
Author: Eddy Date: Thu Mar 29 16:20:59 2012 +0800 (89 minutes ago)

Commit subject: commit 1

Compared with the branch merge results in section 4, the entire git project is indeed much more neat.

8. References

[1] Pro git

 

 

 

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.