Git cheatsheet
Configure tooling
Configure user information for all local repositories
- Set the name you want attached to your commit transactions
1
git config --global user.name "[name]"
- Set the email you want attached to your commit transactions
1
git config --global user.email "[email address]"
- Enable helpful colorization of command line output
1
git config --global color.ui auto
Create repositories
Start a new repository or obtain one from an existing URL
- Creates a new local repository with the specified name
1
git init [project-name]
- Downloads a project and its entire version history
1
git clone [url]
Make changes
Review edits and craft a commit transaction
- Lists all new or modified files to be committed
1
git status
- Snapshots the file in preparation for versioning
1
git add [file]
- Unstages the file, but preserve its contents
1
git reset [file]
- Shows file differences not yet staged
1
git diff
- Shows file differences between staging and the last file version
1
git diff --staged
- Records file snapshots permanently in version history
1
git commit -m "[descriptive message]"
- Add more changes and change message
1
git commit --amend -m "added file and changed message to this"
- Revert a commit in a new commit
1
git revert commit-id
Group changes
Name a series of commits and combine completed efforts
- Lists all local branches in the current repository
1
git branch
- Creates a new branch
1
git branch [branch-name]
- Switches to the specified branch and updates the working directory
1
git checkout [branch-name]
- Combines the specified branch’s history into the current branch
1
git merge [branch]
- Deletes the specified branch
1
git branch -d [branch-name]
Synchronize changes
Register a repository bookmark and exchange version history
- Downloads all history from the repository bookmark
1
git fetch [bookmark]
- Combines bookmark’s branch into current local branch
1
git merge [bookmark]/[branch]
- Uploads all local branch commits to GitHub
1
git push [alias] [branch]
- Downloads bookmark history and incorporates changes
1
git pull
Refactor filenames
Relocate and remove versioned files
- Deletes the file from the working directory and stages the deletion
1
git rm [file]
- Removes the file from version control but preserves the file locally
1
git rm --cached [file]
- Changes the file name and prepares it for commit
1
git mv [file-original] [file-renamed]
Save fragments
Shelve and restore incomplete changes
- Temporarily stores all modified tracked files
1
git stash
- Lists all stashed changesets
1
git stash list
- Restores the most recently stashed files
1
2
3git stash pop
# or either
git stash apply - Discards the most recently stashed changeset
1
git stash drop
Supress tracking
Exclude temporary files and paths
- a text file named .gitignore supresses accidental versioning of filesand path matching the specified patterns
1
2
3*.log
build/
temp-* - list all the ignored files in this project
1
git ls-files --other --ignored --exclude-standard
Redo commits
Erase mistakes and craft replacement history
- Undoes all commits after [commit], preserving changes locally
1
git reset [commit]
- Discards all history and changes back to the specified commit
1
git reset --hard [commit]
- Remove new/untracked files and directories
1
git clean -f
Review history
Browse and inspect the evolution of project files
- Lists version history for the current branch
1
git log
- Lists version history for a file, including renames
1
git log --follow [file]
- Shows content differences between two branches
1
git diff [first-branch]...[second-branch]
- Outputs metadata and content changes of the specific commit
1
git show [commit]