ab2231ss
ab2231ss

Reputation: 13

Git - How do I show file content for each file in the commit?

How can I show file content for each file in the commit?

For example, say the commit has 20 files changed. Right now, I'm using git show *commit*:*file_path* 20 times for each file.

Is there a way to get all this information in just one git command?

Upvotes: 1

Views: 84

Answers (2)

torek
torek

Reputation: 489818

The easiest way is to just check out that commit, then look at those files (and ignore any other files). Note that you can check out to a different work-tree, e.g., using git worktree add (available since Git version 2.5, mostly reliable as of 2.6.x, but best to be on 2.8+ probably):

$ git worktree add /tmp/tmpbranch HEAD~3
Preparing /tmp/tmpbranch (identifier tmpbranch)
HEAD is now at ...
$ ... work with files in /tmp/tmpbranch ...

You can see what you have with:

$ git worktree list
/home/torek/[path]    d22d10a [master]
/tmp/tmpbranch        b6fc8a3 (detached HEAD)

and clean up like this:

$ rm -rf /tmp/tmpbranch
$ git worktree prune

Note that you can place the new work-tree anywhere, but I would put it somewhere out of the current work-area (or even in /tmp like this) just to avoid confusing myself.

(I called this tmpbranch, but using HEAD~3 as the commit-specifier caused it to become detached. Without that, Git would have checked out the HEAD commit under the new branch name tmpbranch. Giving a branch name as the commit-specifier checks out that branch in the new work-tree, unless you add --detach to get a detached HEAD. Using a raw commit hash should get you a detached HEAD every time.)

Upvotes: 1

Gavindra Kalikapersaud
Gavindra Kalikapersaud

Reputation: 547

You could rebase interactively your commits and squash them. Then do git show for that one commit hash. For instance, you could do the following:

git rebase -i HEAD~20
# your terminal will display either nano editor or vim. The format will be as such
# pick *commit_hash* *commit_message*
# pick *commit_hash* *commit_message*
# pick *commit_hash* *commit_message*
# pick *commit_hash* *commit_message*

#change all of your 'pick' to 's' (s means squash). Start from the bottom and go up
#Save your changes on which ever editor you are using. Git will prompt you to name your squash commit
# Then just do git show *commit_hash*

Hope this helps!

Upvotes: 0

Related Questions