10 More Git Commands Everyone Should Know
Amend your last commit
git commit -m "first commit"
git add my_file1
git commit --amend
You end up with a single commit. You must not have pushed the brach somewhere
Discard changes in working directory
The recommended command is “git restore”. The old command is “git checkout” Discard changes of a file in working directory
git restore <file>
or
git checkout <file>
(this is a bit ambiguous, is <file> a branch or a file?)
or
git checkout -- file
Unstage one file, but leave its actual modifications untouched
git restore --staged <file>
or
git reset HEAD <file>
or
git reset <file>
(this can be ambiguous, is it a tag or a file path?)
or
git reset -- <file>
(-- tells git that what comes afterwards is a file path)
Ustage all files from stage area
git reset
or
git reset HEAD
or
git restore .
Restore a file from a previous commit
git restore --source 43534f index.html
git restore --source master~2 index.html
git checkout 43534f -- index.html
Unstage all files from stage area and delete working directory modifications
git reset --hard
Delete the last 3 commits
Git reset HEAD~3
Undo the changes of one commit by creating another commit
Git revert hash
Remove untracked files from the working tree
git clean -nfd : dry run, force, recursive
git clean -fd : force, recursive
Show a diff of a file between two commits
git diff HEAD~2 HEAD -- <file>
git difftool --tool=vimdiff HEAD~1 HEAD <file>
check differences between two branches
git diff master dev
check differences between two commits
git diff b722f1650b9fb33e0990beec027c097526c61478 eab54d0ec21a1d7e351fee4c67139
check differences between current and previous commit
git diff HEAD HEAD~1
or
git diff HEAD~1
check differences between the HEAD and the second to last commit
git diff HEAD~2
check differences between the HEAD (current state/latest revision) and a previous commit:
git diff HEAD b722f1650b9fb33e0990beec027c097526c61478
check what changed since yesterday or a specific date
git diff @{yesterday}
git diff @{2019-01-01}
check all changes to a specific file:
git log --follow /path/to/file
show your remote server
git remote -v
add a remote
git remote add remote-name https://github.com/repo-name
get changes from a remote
git fetch <remote>
if you want to merge them with your current branch
git pull
push your changes to the remote
git push <remote> <branch>
e.g.
git push origin master
rename remote
git remote rename remote-oldname remote-new-name
remove remote
git remote remove remote-name
To see the commit history restricted to only commits affecting a file use
git log -- filename
Show log for a specific branch
git log master
Stash files
Lest say you are in the middle of some changes you need to switch to another branch to fix a bug You should never commit half-done work. You can save your work in a temp area called stash
git stash
git stash list
git stash apply stash@{0}
git stash drop stash@{0}
or
git stash pop