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
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
git status
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."
git status
git log --oneline
Now, let’s switch back to our master
branch.
git checkout master
git log --oneline
Notice that our latest commit doesn’t show up! It was only in our feature
branch.
Look in your directory, too.
ls -a
Notice that file3.txt
doesn’t exist! How do we get it back? Switch back to the feature
branch.
git checkout feature
ls -a
And it’s back!
Now, let’s add a change to the master
branch.
git checkout master
echo "Some more text." >> file2.txt
git add file2.txt
git commit -m "Modifying file2."
git status
git log --oneline
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
Now, we need to merge from the feature
branch.
git merge feature -m "Merging feature into master."
git status
ls -a
cat file3.txt
cat file2.txt
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
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
git branch -d feature
git branch
Now, what happens to the commit history now that we deleted the branch?
git log --oneline
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).