Tutorial Git Branch: Difference between revisions

From HaFrWiki42
Jump to navigation Jump to search
Line 52: Line 52:


== Creating the branch ==
== Creating the branch ==
So far, the repository has one branch with two commits, where each commit introduced a new file.
<br>Next, let’s change to a different branch and modify the first file.
<syntaxhighlight lang="bash">
# Create branch 'alternate' with starting point 'master previous, one commit behind the current head'.
$ git checkout -b alternate master^
Switched to a new branch 'alternate'
$ git show-branch
* [alternate] Initial 3 lines
! [master] Another file
--
+ [master] Another file
*+ [alternate] Initial 3 lines
</syntaxhighlight>
Here, the ''alternate branch'' is initially forked from the ''master^'' commit, one commit behind the current head.
Make a trivial change to the file so you have something to merge, and then commit it.
<br>'''Remember''', it’s best to commit outstanding changes and start a merge with a clean working directory.
<syntaxhighlight lang="bash">
$ cat >> file
Line 4 alternate stuff
^D
$ git commit -a -m "Add alternate's line 4"
[alternate 2a7eab5] Add alternate's line 4
1 file changed, 1 insertion(+)
</syntaxhighlight>
Now there are '''two branches''' and each has different development work.
<br>A second file has been added to the master branch, and a modification has been made to alternate the branch.
<br>Because the two changes <u>do not affect the same parts of a common file</u>, a merge should proceed smoothly and without incident.


== See also ==
== See also ==

Revision as of 17:26, 10 July 2017

Shows the git branch and the git merge commands in detail.
The example is a written example from Version Control with Git [1].


Introduction

A merge unifies two or more commit history branches.
Most often, a merge unites just two branches, although Git supports a merge of three, four, or more branches at the same time.

Preparing for a Merge

Before you begin a merge, it’s best to tidy up your working directory.
During a normal merge, Git creates new versions of files and places them in your working directory when it is finished.
Furthermore, Git also uses the index to store temporary and intermediate versions of files during the operation.

If you have modified files in your working directory or if you’ve modified the index via git add or git rm, then your repository has a dirty working directory or index.
If you start a merge in a dirty state, Git may be unable to combine the changes from all the branches and from those in your working directory or index in one pass.

Setup

<syntaxhighlight lang="bash"> $ mkdir /Test/gitbranch $ cd /Test/gitbranch $ git init Initialized empty Git repository in /Test/gitbranch/.git/ $ git config user.email "hafr@example.nl" $ git config user.name "Harm Frielink"

$ cat > file Line 1 stuff Line 2 stuff Line 3 stuff ^D

  1. Adding and committing

$ git add file $ git commit -m "Initial 3 line file" [master (root-commit) 4530cfd] Initial 3 lines

1 file changed, 3 insertions(+)
create mode 100644 file
  1. Let's add another file

$ cat > other_file Here is stuff on another file! ^D

  1. Adding and committing

$ git add other_file $ git commit -m "Another file" [master b11d58a] Another file

1 file changed, 1 insertion(+)
create mode 100644 other_file

</syntaxhighlight>

Creating the branch

So far, the repository has one branch with two commits, where each commit introduced a new file.
Next, let’s change to a different branch and modify the first file. <syntaxhighlight lang="bash">

  1. Create branch 'alternate' with starting point 'master previous, one commit behind the current head'.

$ git checkout -b alternate master^ Switched to a new branch 'alternate'

$ git show-branch

  • [alternate] Initial 3 lines
! [master] Another file

--

+ [master] Another file
  • + [alternate] Initial 3 lines

</syntaxhighlight>

Here, the alternate branch is initially forked from the master^ commit, one commit behind the current head.

Make a trivial change to the file so you have something to merge, and then commit it.
Remember, it’s best to commit outstanding changes and start a merge with a clean working directory. <syntaxhighlight lang="bash"> $ cat >> file Line 4 alternate stuff ^D $ git commit -a -m "Add alternate's line 4" [alternate 2a7eab5] Add alternate's line 4

1 file changed, 1 insertion(+)

</syntaxhighlight>

Now there are two branches and each has different development work.
A second file has been added to the master branch, and a modification has been made to alternate the branch.
Because the two changes do not affect the same parts of a common file, a merge should proceed smoothly and without incident.

See also

top

Reference

top

  1. Version Control with Git, Powerful Tools and Techniques for Collaborative Software Development, Jon Loeliger & Matthew McCullough, O'Reilly, 2012 Second Edition, isbn=9781449316389