How do i look at differences between different commits in git?
-
The special term HEAD is a pointer to the most current commit of your changes in git, and you can use this to compare the current commits with previous commits. Think of commits as mini snapshots, rather like you would snapshot a disk or virtual machine, you can get back to a previous state.
You can rewind back from the current HEAD position to previous commits, so look at this following, these are all my commits, most recent being id: cc9e080, reverse chronological order.
$ git log --oneline cc9e080 I want to keep the changes to my fith line 08c6bb5 My Super duper forth line message afa0b05 More than one line this is another line dbaade7 Added a second line to my file 2561e02 Initial commit of repository
Let's say I want to see the differences between, current position of the HEAD and one commit back, i.e what I have in my file or folders currently, and what I had before I made my last change, that difference is I added the fifth line, with the "+" pluses, difference lines are usually colour coded green. I guess you can look at the pluses as additions to the previous commit. The tilde is interpreted by git.
$ git diff HEAD~1 diff --git a/README.md b/README.md index 4ba7d0a..9f28333 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,5 @@ Second Line: When I get back from running I'm tired Third Line: I got straight to bed Fourth Line: and I dream of running the marathon + +Fifth Line: but I don't dress up in a silly costume I'm a serious runner
We can keep going back too, two commits behind is:
$ git diff HEAD~2 diff --git a/README.md b/README.md index 1408e61..9f28333 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,7 @@ First Line: Today i woke up and went running Second Line: When I get back from running I'm tired Third Line: I got straight to bed + +Fourth Line: and I dream of running the marathon + +Fifth Line: but I don't dress up in a silly costume I'm a serious runner
Three commits behind
$ git diff HEAD~3 diff --git a/README.md b/README.md index 23f55ff..9f28333 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ First Line: Today i woke up and went running Second Line: When I get back from running I'm tired + +Third Line: I got straight to bed + +Fourth Line: and I dream of running the marathon + +Fifth Line: but I don't dress up in a silly costume I'm a serious runner
You get the idea. If you got past the total number of commits, it git will show an error.
The HEAD~<number> can become tedious when you got 100's of commits, that's where the commit hash on the left of the git log becomes handy you can use that to do the diff.
$ git log --oneline cc9e080 I want to keep the changes to my fith line 08c6bb5 My Super duper forth line message afa0b05 More than one line this is another line dbaade7 Added a second line to my file 2561e02 Initial commit of repository $ git diff 08c6bb5 diff --git a/README.md b/README.md index 4ba7d0a..9f28333 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,5 @@ Second Line: When I get back from running I'm tired Third Line: I got straight to bed Fourth Line: and I dream of running the marathon + +Fifth Line: but I don't dress up in a silly costume I'm a serious runner
© Lightnetics 2024