git branch management (c)

A branch operation

1 Overview

  Each commit, Git them strung ⼀ timeline, this timeline is ⼀ branches. git branch create a default initialization, that is, the master branch. HEAD strictly speaking is not directed to submit, but point to the master, master is the point of submission, so, HEAD is pointing to the current branch. In the beginning, master branch is a line, Git with master points to the latest submission, and then point to master HEAD, will be able to determine the current branches, and submit point of the current branch.
Here Insert Picture Description

2 Create branch

  Such as creating a new branch dev, Git built a pointer called dev, points at the same master, and then point to HEAD dev, says that the current branch on the dev, create a branch command:

$ git checkout -b dev
Switched to a new branch 'dev'

  git checkout command with the -b parameter indicates and switches, the equivalent of the following two commands:

$ git branch dev
$ git checkout dev
Switched to branch 'dev'

  Then, view the current branch with git branch command:

$ git branch
* dev
master

  git branch command lists all the branches, ahead of the current branch monogram an asterisk.
Here Insert Picture Description
  Git create a branch soon, because in addition to adding a dev pointer to change HEAD point, the workspace file have not changed! From now on, to modify the workspace and submission is for a dev branch, such as new submissions after the first, dev pointer moves one step forward, while the master pointer unchanged:
Here Insert Picture Description

3 branch switching

  If you switch branches:

$ git checkout master
Switched to branch 'master'

Here Insert Picture Description

4 merging branches

  Work on the dev completed, the dev can be incorporated into the master. The easiest way is to point directly to the master dev current submission, it completed the merger. Results of the work dev branches merged into the master branch:

$ git merge dev
Updating d17efd8..fec145a
Fast-forward
readme.txt | 1 +
1 file changed, 1 insertion(+)

Here Insert Picture Description

5 Disable fast forward

  Typically, when merging branches, if possible, Git will use "Fast forward" mode, but this mode, delete the branch, the branch will lose information. If you want to force disable "Fast forward" mode, Git will run when a new merge commit, so, from the branch history can be seen branch information.

$ git merge --no-ff -m "merge with no-ff" dev
Merge made by the 'recursive' strategy.
readme.txt | 1 +
1 file changed, 1 insertion(+)

  --no-ff Parameters, disable "Fast forward", since the merger to create a new commit, so add -m parameter, to commit description written into it.

6 deleted branches

  So Git merging branches soon! Changed hands, workspace content is the same! After merging branches, or even delete the dev branch. Dev branch is to delete the dev pointer to delete it, delete, we only have the master branch.

$ git branch -d dev
Deleted branch dev (was fec145a).
$ git branch
* master

Two to resolve conflicts

Here Insert Picture Description
  In this case, Git can not perform "quick merger" can only try to merge their modifications, but such a merger might be conflicts:

$ git merge feature1
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#
# Unmerged paths:
# (use "git add/rm <file>..." as appropriate to mark resolution)
#
# both modified: readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

  View the contents of the file:

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 with <<<<<<<, =======, >>>>>>> mark the contents of different branches, re-submitted revised conflict.
Here Insert Picture Description
  We can also see the consolidation of the branch with git log with parameters:

$ git log --graph --pretty=oneline --abbrev-commit
* 59bc1cb conflict fixed
|\
| * 75a857c AND simple
* | 400b400 & simple
|/
* fec145a branch test
...

  With the git log --graphcommand to see the branch merged graph.

Three-branch strategy

  In the actual development, branch management should follow a few basic principles: First, a branch of the project is divided into three categories: a master branch, a sub-branch of the master branch, and individual branch (everyone has a branch) inferior; Secondly, master branch should be very stable, which is only used to release a new version, usually can not work on it; work in sub-branch, that branch is unstable, at some point, such as when the 1.0 release, and then merged into the master branch, released version 1.0 in the master branch; and finally, everyone working on the dev branch, everyone has their own branch, from time to time to dev branch merge it . Therefore, the branch of teamwork looks like this:
Here Insert Picture Description

Four branch Bug

  Software development, you need to have a bug fix, in Git, each bug can all be fixed by a new temporary branch, after the repair, the combined branch, and then delete the temporary branch.
  But many times, the present work can not submit, you can use the stash function.
  In fact, in the development of new features, bug fixes and concepts, as it is the proposed re-establishment of a branch branch function, and then merge, delete the branch, if you want to discard a branch had not merged, v can be git branch -D nameforcibly removed.

Git five of M, T, D, A, U flag

  In use git checkout, git status, git diff files or wonder when the logo appears. As M, T, D, A, R, U and the like.

$ git checkout master
M cpp-iniparser

  Foregoing documents have cpp-iniparser M. git of these strange signs is what this means.
  A: increased file.
  C: A new copy of the file.
  D: a deleted file.
  M: contents of the file or the mode is changed.
  R: file name is changed.
  T: type of the file has been modified.
  U: files are not merged. (You need to complete the merger before committing)
  the X-: an unknown state. (Git probably encounter a bug, you can submit a bug report to git)
  man git diff-files can be found in the description of these flags in the manual git diff-files.
  These status flags are defined in the file diff.h git source code.

Six branches switched to commit not add or content with past

  git will not add content switching branch or commit with the past, because not add content does not belong to any branch, did not commit any of the content does not belong to a branch. That is, for all the branches, the work area and the staging area is public.
  To switch between branches, and do not want these effects, how to do it? git stashHuh. Note that branch git stash in the current content, in other branches can also git stash pop out, and why? Because: the work area and the staging area is public.
  Another method is to use git addand git commitcommit changes, as long as the git statusinspection area and the staging area is clean it.

Published 67 original articles · won praise 26 · views 80000 +

Guess you like

Origin blog.csdn.net/essity/article/details/100975840