The Difference Between ∼ and ^

Learn the difference between "~" and "^".

These two operators are used in a very similar way, so the difference between the two can be obscure.

Understanding differences by example#

Here’s a demonstration of it in its simplest form with another example:

1   mkdir -p lgthw_bisect_2
2   cd lgthw_bisect_2
3   git init
4   touch afile
5   git add afile
6   git commit -am 'Initial commit'
7   git branch abranch
8   git checkout abranch
9   echo 'abranch addition' >> afile
10  git commit -am 'abranch addition'
11  git checkout master
12  echo 'master addition 1' >> afile
13  git commit -am 'master addition 1'
14  echo 'master addition 2' >> afile
15  git commit -am 'master addition 2'
16  git merge -X ours -m merged abranch
Terminal 1
Terminal

Click to Connect...

Explanation#

What happened there? You created an initial commit of a single empty file and then branched to a branch called abranch. You made a change on that branch, then checked out master, and made two changes there.

The -X ours flag#

One thing worth explicitly calling out here is the -X ours flag in the git merge command. When merging, you can tell Git to use specific strategies to help it decide how to merge and reduce the chances of having to resolve a conflict. In this case, when faced with a conflict, Git will favor changes made within the current branch over the one being merged in.

So you end up with a tree that looks like this:

A simple branched and merged repository

Check this yourself and compare it to the output of the git log command. Type this command in the terminal provided above.

17  git log --oneline --graph --all

Now you will try to show all the items using ^ and ~. You’re going to have to think through the difference between ^ and ~ below by yourself. And try some commands of your own.

It’s the only way to truly learn.

18  git show HEAD^
19  git show HEAD^^
20  git show HEAD^2
21  git show HEAD~1
22  git show HEAD~2
23  git show HEAD~2^1
Terminal 1
Terminal

Click to Connect...

Did you figure it out? If not, study the diagram again and look carefully at what git show reported. Also, use git log commands to work out what’s going on.

If all else fails, then start the subsection here again. Then look at the man pages AND then google it to see if that makes more sense.

If it still doesn’t make sense, then make a note to look at it again at a later date.

This is how I learned a lot in Git.

A Real 'git bisect' Session
Introduction: Fetching and Pulling Content
Mark as Completed
Report an Issue