Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to see old version of a file (before it was added to the staging area/index)?

Tags:

git

Suppose I changed my file foo.txt and run git add foo.txt. Now foo.txt appears in the list of the "changes to be committed".

Now I would like to see my foo.txt before these changes. How can I do it with git?

like image 738
Michael Avatar asked Sep 18 '11 14:09

Michael


People also ask

Which command is used to explore the previous versions of a project?

git checkout recovers old versions of files.

How do I see previous commit files?

Find what file changed in a 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.


2 Answers

You can do the following:

git show HEAD:foo.txt

In general, this syntax is very useful for seeing a file from a particular commit without touching your working tree. For example, if you want to see what README.txt was like in the grand-parent of commit f414f31 you can do:

git show f414f31^^:README.txt

Update: as VonC comments below, it's important to note here that the path here must be the full path from the root of the working tree, even if you're currently in a subdirectory.

However, when staging changes, one does tend to be more often interested in differences, which is what Abizern interpreted your question as asking about. A simple way of thinking about those commands is:

  • git diff means "what changes haven't I staged yet?"
  • git diff --cached means "what changes have I already staged?"
like image 51
Mark Longair Avatar answered Oct 19 '22 19:10

Mark Longair


I think what you're asking is about diffs (which show the differences between versions of files)

I've written about them here

But a summary diagram is:

enter image description here

like image 38
Abizern Avatar answered Oct 19 '22 17:10

Abizern