Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing changes in commits to the current file by Git

Tags:

How can you show the differences of a file in the last 5 commits to the current uncommitted file by Git-show?

I made a change to my file which breaks my code. However, I do not know where the change is.

I would like to compare the current uncommitted files to the recent commit (HEAD), to the previous commit (^HEAD) and at least 3 commits deeper.

However, I do not know how you can do it efficiently.

In trying to see the changes of the five last commits of one file to the current file in the given branch, I unsuccessfully ran

git show next~5:handle_questions.php 
like image 808
Léo Léopold Hertz 준영 Avatar asked Aug 20 '09 00:08

Léo Léopold Hertz 준영


People also ask

How do I compare files between two commits?

You can compare files between two Git commits by specifying the name of the ref that refers to the commits you want to compare. A ref may be a commit ID or HEAD, which refers to the current branch. Let's compare two commits in our Git repository. The above command will perform a diff operation across our two commits.

How do I see files changed in a specific commit?

To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.

Which git command shows the changes between commits?

Diffing is a function that takes two input data sets and outputs the changes between them. git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.

How do I compare changes between two Git commits?

I hope you're now able to compare the changes and recognize what we changed by looking at the diff's outcome. diff is a super powerful command that lets you compare changes in lots of ways. You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit.

How do I compare two repositories in Git?

Here's an example of a comparison between two repositories. As a shortcut, Git uses the ^ notation to mean "one commit prior." You can use this notation to compare a single commit or branch against its immediate predecessors. For example, 96d29b7^^^^^ indicates five commits prior to 96d29b7, because there are five ^ marks.

How to get the Sha of a specific commit in Git?

Check $ git log, then copy SHA id of 2 different commits and then run git diff command with those ids, for example: If you want the diff for a specific file, add the path to it at the end of the command. Also do a "git pull" to download the full tree info if the two commits are across diffrerent branches.

How do I commit staged changes in Git?

Fine, first we commit the staged changes by git commit -m "intro to cat and dog": Now, stage the "puppy" to "pup" change. Then, run the git diff --staged command which lists out the changes between the staged area and your last commit. A version – last commit containing the line my name is puppy in dog.txt


2 Answers

Here is my cheat-sheet:

# uncommited file to HEAD git diff <path>  # uncommited file to before last commit git diff HEAD^ -- <path>  #last commit to before last commit git diff HEAD^ HEAD -- <path>  #difference between HEAD and n-th grandparent git diff HEAD~n HEAD -- <path>  #Another cool feature is whatchanged command git whatchanged -- <path> 
like image 166
db_ Avatar answered Oct 18 '22 08:10

db_


To see the diff between handle_questions.php in the working directory and in the repository 5 commits back, use:

 $ git diff HEAD~5 handle_questions.php 
like image 23
William Pursell Avatar answered Oct 18 '22 10:10

William Pursell