Table of Contents
Git - Commits Revisited
Commit In Detail
Improve the previous commit:
git commit --amend
this will add any changes in the index to the commit you just made. Additionally you may edit the commit message.
Change the author and/or email address of your last commit (if you e.g. accidentally used the wrong email address)
# change your .gitconfig file, set a new name and email: git config --global user.name "New Username" git config --global user.email email@example.com git commit --amend --reset-author # or give the new username and email address on the command line: git commit --amend --author="New Username <firstname.lastname@example.org>"
Interactive Patch Mode
To just add portions (patches) of a modified file you could use the following command:
commit add -p file
This will split the current file into hunks if you want and enters interactive mode. Your options are:
|y||yes, use hunk|
|n||no, don't use hunk|
|q||quit, don't use the current or any following hunk|
|a||use the current and all remaining hunks|
|s||split, try to split the current hunk into more pieces|
|e||edit the current hunk|
Restore files from a former commit:
git checkout [-f] <reference> -- <glob> # -f will overwrite files # reference is your sha-reference # glob can be a directory (files will be overwritten)
git checkout -- myfile # replace all changes you made to myfile with the version stored in HEAD of the current branch git checkout ab987765 -- myfile # replace myfile with an older version than HEAD
git show ab987765:myfile # show the contents of an older version of myfile
Undo a change you made
git revert ab987765 # undo the given commit WITHOUT deleting it. A new commit will be created # which will revert what you did before.
This should only be done for local changes!
# using --soft will keep your changes git reset --soft HEAD^ # using --hard will throw away the changes you made git reset --hard HEAD^
If you cluttered your working directory with untracked files
git clean comes to the rescue:
# dry run, with directories (-d) git clean -nd # force a run: git clean -f # remove directories too: git clean -fd
Be careful: theres no way back!
A detached HEAD points directly to a commit - instead of pointing to it through a branch. So there is no reference to this commit - it will be unreachable via normal refs.
To solve that problem, go through the following steps:
# assumption: you are currently on the detached head git branch temp git checkout temp # this will attach the detached head to the new branch temp # optionally you might want to examine your current commit with the # branch you thought you were working on git diff master temp git diff origin/master temp # if everything is as expected, point the master branch to it: git branch -f master temp git checkout master # get rid of the temp branch: git branch -d temp # fixed :)
Show Log of Commits Which Modified a Given File
# show plain log git log -- path/to/file # add diff git log -p -- path/to/file