Branches

As promised, I said I would get back to the cryptic comment in the git status saying On branch master. Well, here’s the point where I deliver on my promise.

Your repository can save multiple histories. Like alternate timelines from some episode of Star Trek, you can store different sequences of changes in different branches.

If you haven’t run the previous Repositories section, then let’s get back to our sample project repository.

export TERM=xterm-mono
cd ~/my_project

Note: If you get an error here, go back to the previous section (“Repositories”) and create the sample repository following that notebook.

Now, let’s create a feature branch.

git branch feature
git status
On branch master
nothing to commit, working tree clean

Note that we are still on the master branch, though! To change to the new branch we just created, you need to use git checkout.

git checkout feature
Switched to branch 'feature'
git status
On branch feature
nothing to commit, working tree clean

Now, let’s add a new file in this branch.

echo "And another file." > file3.txt

And we stage and commit this new file to this branch.

git add file3.txt
git commit -m "Adding file3."
[feature cdcb791] Adding file3.
 1 file changed, 1 insertion(+)
 create mode 100644 file3.txt
git status
On branch feature
nothing to commit, working tree clean
git log --oneline
cdcb791 (HEAD -> feature) Adding file3.
8ab55cd (master) Modifying file1.
f5ee173 Adding file2.
52cc926 Adding file1

Now, let’s switch back to our master branch.

git checkout master
Switched to branch 'master'
git log --oneline
8ab55cd (HEAD -> master) Modifying file1.
f5ee173 Adding file2.
52cc926 Adding file1

Notice that our latest commit doesn’t show up! It was only in our feature branch.

Look in your directory, too.

ls -a
.		..		.git		file1.txt	file2.txt

Notice that file3.txt doesn’t exist! How do we get it back? Switch back to the feature branch.

git checkout feature
Switched to branch 'feature'
ls -a
.		.git		file2.txt
..		file1.txt	file3.txt

And it’s back!

Now, let’s add a change to the master branch.

git checkout master
Switched to branch 'master'
echo "Some more text." >> file2.txt
git add file2.txt
git commit -m "Modifying file2."
[master f4dd054] Modifying file2.
 1 file changed, 1 insertion(+)
git status
On branch master
nothing to commit, working tree clean
git log --oneline
f4dd054 (HEAD -> master) Modifying file2.
8ab55cd Modifying file1.
f5ee173 Adding file2.
52cc926 Adding file1

So, now we have 2 branchs, and each branch shares the first 3 commits in common, but each branch has a different commit added at the “end.”

Merging

These two branches can continue to develop independently from each other for as long as your like. However, usually the point of having a branch is because you intend the other branch’s changes to get merged back into the master branch, at some point. It’s like following 2 independent thought processes to their natural conclusion, making a decision, and then choosing the process (branch) that you want to use and merging it into your process. …If that makes sense.

Let’s merge our feature branch into our master branch.

To do this, we need to be in the master branch, where we want the new commits to be merged.

git checkout master
Already on 'master'

Now, we need to merge from the feature branch.

git merge feature -m "Merging feature into master."
Merge made by the 'recursive' strategy.
 file3.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 file3.txt
git status
On branch master
nothing to commit, working tree clean
ls -a
.		.git		file2.txt
..		file1.txt	file3.txt
cat file3.txt
And another file.
cat file2.txt
This is a new file.
Some more text.

And now you can see we have all of the changes we made to the feature branch in our master branch, and all of the changes that were only in our master branch are still there.

Let’s look at the history of our master branch, now.

git log --oneline
5b75c02 (HEAD -> master) Merging feature into master.
f4dd054 Modifying file2.
cdcb791 (feature) Adding file3.
8ab55cd Modifying file1.
f5ee173 Adding file2.
52cc926 Adding file1

Notice that the 4th commit (Adding file3.) also indicates that it came from the feature branch, even though we are looking at the history of the master branch.

Deleting branches

Now that our feature branch has been merged into master, it is usually good practice to delete it. You actually don’t have to delete this branch, but usual practice is to only keep active branches around. Otherwise, you run the risk of your repo behind hard for new users to parse and figure out.

To delete the feature branch, we use the -d option to git branch:

git branch
  feature
* master
git branch -d feature
Deleted branch feature (was cdcb791).
git branch
* master

Now, what happens to the commit history now that we deleted the branch?

git log --oneline
5b75c02 (HEAD -> master) Merging feature into master.
f4dd054 Modifying file2.
cdcb791 Adding file3.
8ab55cd Modifying file1.
f5ee173 Adding file2.
52cc926 Adding file1

Notice that the commit log is the same, except that knowledge that the 4th commit came from the feature branch has now been lost (but the commit has not).