Git Cheat Sheet

Commands

git commit -a: Stages files automatically

# Stage all modified files in the repository
$ git commit -a

# Stage all modified files in the repository and provide a commit message
$ git commit -a -m "Commit message"

git log -p: Produces patch text

# Show the patch text for all commits in the repository
$ git log -p

# Show the patch text for the last 5 commits in the repository
$ git log -p -5

git show: Shows various objects

# Show the details of the latest commit in the repository
$ git show

# Show the details of a specific commit in the repository
$ git show <commit-hash>

git diff: Is similar to the Linux `diff` command and can show the differences in various commits

# Show the differences between the current branch and the latest commit
$ git diff

# Show the differences between two specific commits
$ git diff <commit-hash1> <commit-hash2>

git diff --staged: An alias to --cached, this will show all staged files compared to the named commit

# Show the differences between the staged files and the latest commit
$ git diff --staged

git add -p: Allows a user to review patches to add to the current commit interactively

# Review patches interactively before adding to the current commit
$ git add -p

git mv: Similar to the Linux `mv` command, this moves a file

# Move a file in the repository
$ git mv <old-file-name> <new-file-name>

git rm: Similar to the Linux `rm` command, this deletes, or removes a file

# Remove a file from the repository
$ git rm <file-name>

# Remove a file and stage the deletion for commit
$ git rm -f <file-name>

There are many useful git cheatsheets online as well. Please take some time to research and study a few, such as this one.

.gitignore files

.gitignore files are used to intentionally tell the git tool to ignore some files in a given Git repository. For example, this can be useful for configuration or metadata files that a user may not want to check into the master branch. Check out more at: https://git-scm.com/docs/gitignore.

A few common examples of file patterns to exclude can be found here.

Git Branches and Merging Cheat Sheet


git branch

Used to manage branches

git branch <name>

Creates the branch

git branch -d <name>

Deletes the branch

git branch -D <name>

Forcibly deletes the branch

git checkout <branch>

Switches to a branch.

git checkout -b <branch>

Creates a new branch and switches to it.

git merge <branch>

Merge joins branches together.

git merge --abort

If there are merge conflicts (meaning files are incompatible), --abort can be used to abort the merge action.

git log --graph --oneline

This shows a summarized view of the commit history for a repo.

Git Revert Cheat Sheet


git checkout is effectively used to switch branches.

git reset basically resets the repo, throwing away some changes. It’s somewhat difficult to understand, so reading the examples in the documentation may be a bit more useful.

Some other useful online articles discuss more aggressive approaches to resetting the repo.

git commit --amend is used to make changes to commits after-the-fact, which can be useful for making notes about a given commit.

git revert makes a new commit which effectively rolls back a previous commit. It’s a bit like an undo command.

There are a few ways you can roll back commits in Git.

There are some interesting considerations about how git object data is stored, such as the usage of sha-1.

Feel free to read more here: