Some tasks using Git version control

This is a page of memos about how to do various tasks with the Git version control system. Disclaimer: I am not a git expert. Please exercise caution when you use the advice given here. If you have any comments, email me at the address at the bottom of the page.

Run a script before commiting

To run a script before each commit, add a script with the name pre-commit to the directory .git/hooks.

Fix a merge conflict

git pull
edit files with editor to fix problems
git add
git commit

See Git: You are in the middle of a conflicted merge.

Clone a directory

To clone all the files in dir1 to dir2,

git clone dir1 dir2

Compare last commit and the one before that

To compare the last commit and the one before that,

git diff HEAD^ HEAD

Show a file at a particular revision

To show a file at a particular revision,

git show 5ca5d4b7028824cec4dc536ea51a9533627e6805:file
where the number here is the reference from the commitment. You can get the numbers using git log.

Retrieve accidentally deleted files

This restores files you have deleted using rm and similar.

git ls-files -d | xargs git checkout --

See Git saved my day - restore accidentally deleted files using Git version control and a Linux mailing list.

Get a diff after adding but before committing

git diff --cached

See Git by Example - SysMonBlog.

Web links

Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer