Tutorial Git Branch
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
- 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
- Let's add another file
$ cat > other_file Here is stuff on another file! ^D
- 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>
Setup
Setup a new repo with the initial data.
<syntaxhighlight lang="bash"> $ mkdir gitbranch $ cd gitbranch
- Initialize git.
$ git init Initialized empty Git repository in /Test/gitbranch/.git/
- Creating the initial content
$ echo Original Init Data Master >> data $ git add data $ git commit -m "Introduction" [master (root-commit) 551517e] Introduction
1 file changed, 1 insertion(+) create mode 100644 data
- Check the status
$ git status On branch master nothing to commit, working directory clean </syntaxhighlight>
Create branch
Now lets create a new branch called newImage. <syntaxhighlight lang="bash">
- Creating the new branch
$ git branch newImage
- Prepare to put something into the new branch
$ git commit $ git branch
* master newImage
</syntaxhighlight>
Houston we have a problem. We are at branch master instead of the newImage.
See also
- Git, General on Git
- GitHub, General on GitHub
- Tutorial Git Add, Tutorial on 'git add'.
- Tutorial Git Commit, Tutorial on git commit.
- Tutorial Git Diff, Tutorial on 'git diff'
Reference
- ↑ Version Control with Git, Powerful Tools and Techniques for Collaborative Software Development, Jon Loeliger & Matthew McCullough, O'Reilly, 2012 Second Edition, isbn=9781449316389