git diff allows you to compare different versions of a file against each other.
- On its own,
git diffcompares the working directory to the staged version of the file.
git diff --stagedcompares the staged version to the most recent commited version.
git diff HEADcombines the changes in your working and staged versions of the file, and compares them to the version of the file designated as HEAD (most often, the most recent commit).
git diff --color-wordsdisplays a word-by-word comparison rather than a line-by-line comparison, helpful for small changes.
Viewing Project History
git logprovides a list of all of the commits on the current branch, with the most recent commit first. Up and down arrows navigate, enter cycles through log entries, and q quits log viewing screen.
git log --onelineshows a smaller version of the log.
git log --oneline --graphshows a graph of the changes along with the one-line log.
git log --oneline --graph --decorateincludes information about the branches and the head.
git log --oneline --graph --decorate --allalso includes unmerged branches.
git log --stattells you which files were included in each commit.
git log --patchshows the actual changes that were made in each commit as diffs
git revert <commit_hash>to revert your last commit
git commit -amendto fix the last commit's message or add more files to that commit.
git checkout --<filename>is the syntax. The
--lets Git know you're talking about a file. This will revert the file to the version found in the last commit.
git reset HEAD <filename> will remove a file from the staging area, putting it back in your working directory.
Git Reset Modes
The three modes for git reset are:
git revertreverts a specific commit.
git resetresets git to a specific commit.
git reset --soft <to_commit>resets git to a specific commit, and puts the commits you're resetting into the staging area where they can be easily re-committed.
git reset --mixed <to_commit>resets git to a specific commit, and puts the commits you're resetting into the working directory so you can edit them directly.
git reset --hard <to_commit>resets git to a specific commit, and deletes the commits you're resetting.
- Just like with git revert you can use the commit ID, or you can use the syntax
HEAD~<number>. The number you put will be the number of commits backwards from the current HEAD that git will move the new HEAD to.
git reset --soft HEAD~2 # indicates you want to go back two commits.
Then if you recommit these changes you will stage the commits, leaving a cleaner history.
Keywords for closing issues
The following keywords will close an issue via commit message:
Creating Command Shortcuts
- You can create aliases in git that let you call on the short alias instead of writing out a full long command.
- You do this by setting a global alias using
git config --global alias.lol "log --oneline --graph --decorate --all"would let you type
git lolinstead of the entire log command with all its options.
git config --global alias.co "commit -m"would let you write
git co "Commit Message"to quickly commit with a message attached.
Fetching a remote
git fetch upstream
git merge upstream/master