Git Cheat Sheet - CIRA Software Engineering Group
Git Cheat Sheet - CIRA Software Engineering Group
Getting & configuring Git What was I thinking‽ — reviewing history & undoing changes
Git is a freely-available, open-source version control software for Linux, Windows, and Mac. Git gets it — you & your collaborators aren’t always perfect. Browse & inspect how the files in the project
To get Git, visit: https://git-scm.com/downloads. have changed as well as erase your mistakes.
✓ git --version Check the version of your Git installation " git log List version history for the currently checked out branch
Set and display Git local configuration information git log --tags A more human-readable version of git log
--decorate=full
" git config --list Display your current local Git configuration
git log --follow <file> Display the history for a single file
git config --global user.name Sets the username associated with your commits
”<name>”
" git blame <file> Adds user & log information about file changes
git config --global Sets the email you want attached to your commits
user.email ”<email>”
" git diff <commit ID> <file> Changes between a previous commit & current file state
" git config --global color.ui Adds a splash of color to Git to make your life easier git diff Shows the changes in a file between branches. Note
auto <branch 1>...<branch 2> that the ... blends the differences between the two ver-
<file> sions together to make it easier to compare.
git config --global If you are not a fan of your system’s default, you can
core.editor <editor> change the editor (e.g., atom, vim, emacs)
" git reset <file> Removes the <file> from the staging area & keeps local,
unstaged changes. This is a “mixed” mode reset.
Where to start — setting up your repository git reset <commit id> Undoes all commits after <commit id> while keeping local,
Initialize a new local repository, set up a remote, or clone an existing project along with its history. unstaged changes
" git init <repository name> Creates a new local repository git reset --hard <commit id> “Hard” reset mode undoes changes after the <commit id>,
clears the staging area, & rewrites history
git remote add <remote name> Creates a link to a remote repository (e.g., a hosting service
<url> like GitHub or GitLab). <remote name> is an alias for the
<url> & is typically set to ‘origin’. (-f fetches the remote " git revert <commit id> Leaves commit history intact, but restores/copies a previ-
history during the add) ous commit and sets it as the most recent
git clean Removes untracked files from working directory (-n dis-
" git clone <url> Creates a local copy of the repository on your machine plays what will be removed; -f cleans the directory)
git clone -b <branch> <url> Only clones a specific branch git checkout <file> Restore a file to the previous commit
Making a change — stage & snapshot your changes Keeping your repository up-to-date — managing remotes
Review your edits, commit a snapshot, and tag your projects progress. Commands to assist in synchronizing your local repository with your remote. Note: <remote name>
" git status List which files have been modified or staged locally is the alias you set for the remote (e.g., ‘origin’) when you do a git remote add.
" git remote -v Lists the url & name of the remote fetch & push
git show Last commit & subsequent file changes information
git remote show <remote name> Displays the remote url & detailed branch statuses
" git diff <file> Show unstaged changes to your file(s) git remote set-url Changes the url for a specific remote name if it has been
git diff --staged Show staged, uncommitted changes <remote name> <url> previously set by git remote add
git diff --cached Difference between staged changes and the last commit
git fetch <remote name> Fetches the entire repository history from the remote
<branch name> (adding branch only gets that specific branch)
" git add <file> Add a file snapshot to your next commit
" git pull <remote name> Downloads the current working branch & merges it with
" git commit -m ”<message>” Commit your staged content as a new commit snapshot local (to rebase rather than merge, add --rebase)
git commit --amend Replace and/or edit the last commit before git push
" git push -u <remote name> Sends local branch commits to the remote (--tags pushes
" git tag Lists the repository’s tags (-n displays notes & messages) <branch name> tags; --all uploads changes to every local branch)
git tag -a <version number> Flags the code with a version number and message
-m ”<message>”
Git Cheat Sheet — CIRA Software Engineering Group
Organizing patches & features — branches Incorporating snippets of code from other projects — subtrees
Creating branches isolates changes to your coding project while keeping your master branch stable. A friend has an awesome script in version control that you want to add to your shiny, new repository.
" git branch Lists branches in the repository (an * proceeds the cur- Don’t copy it! Subtree it! Subtrees are repositories within repositories. Here are a few notes:
rent/working branch). -a shows remote branch names. • Always run subtree commands from the top/root directory of your repository.
git branch <branch name> Creates a new branch • Try not to modify the subtree’s code in the current repository. But, it can be done if necessary.
git branch -d <branch name> Deletes the specified branch • DO NOT USE a leading or trailing “/” for the subtree directory prefix; dir1/dir2 is safe.
• Always use --squash to keep the subtree repository history out of your history.
" git checkout <branch name> Switches the repository to the user defined branch
" git subtree add Insert a branch of a remote repository into the specified
git checkout -b <branch name> A single step combining creating & checking out a branch --prefix=<subtree directory> subtree directory within your current repository. New re-
<remote name> motes can be set with git remote add.
Incorperating changes into the main branch — merging <remote branch name>
--squash
To merge, or not to merge, that is the question — useful commands to allow you to combine changes in
your code with other branches in your repository. Once the ‘feature’ you added in your branch is stable, git subtree pull Get the updated branch for the remote repository for the
you can merge away! --prefix=<subtree directory> specified subtree
<remote name>
" git merge <branch name> Combine branch of the code you want to insert (add
<remote branch name>
--no-ff to prevent/resolve a “fast-forward”)
--squash
" git remote prune Removes dead wood by cleaning up deleted remote Ingoring patterns & suppressing tracking — .gitignore
<remote name> branches in your local repository
Including a well-thought-out .gitignore file in your repository can save you a lot of time and
headaches. The .gitignore tells Git what patterns to avoid and prevent you from committing to
Spring cleaning — refactoring or removing files & paths your repositories. Below are a few examples:
Need to relocate or completely remove files or directories? Then, we’ve got Git commands for you! " *.nc Adds a wildcard glob to avoid committing files with the ex-
Note: use Git to relocate or remove your file rather than OS-specific commands so that the change is tension .nc
tracked.
temporary_* Adds a wildcard glob for the partial name of a file
" git rm <file or directory> Deletes the file or directory from your local working repos-
itory & stages the deletion for the next commit data/ Excludes the directory data from being committed
git rm --cached <file or Removes the file or directory from version control, but data/*.dat Excludes files with the extension .dat in the directory
directory> keeps your local copy data
" git ls-files -o -i Lists all ignored files in the repository (good to check to
" git mv <source> <destination> Changes the name of the file or directory and stages the
--exclude-standard make sure something you want version controlled isn’t
change for the next commit
being ignored)
" git filter-branch Removes the snapshots of the file from your commit his- git add --force <file> Adds a snapshot for an otherwise ignored file
--tree-filter ’rm -f tory. Warning: rewriting history will open a huge can of
<file>’ HEAD worms — You’ve been warned!
Other useful Git resources
Temporary commits & experimental code fragments — stashing • http://swcarpentry.github.io/git-novice/
• https://rogerdudler.github.io/git-guide/
Putting changes on hold so you can do something else — let’s face it, multitasking is next to impossible. • https://www.atlassian.com/git/tutorials/
" git stash list List all current stash entries (e.g., changesets & stash ids) • https://git-scm.com/docs/gittutorial
• https://try.github.io/levels/1/challenges/1
git stash save ”<stash name>” Save and store your changes on a list • Your favorite internet search engine or question-and-answer site (e.g., Stack Overflow)
git stash pop Applies a stash & removes it from the stash list
stash@{<number>} " Indicates a Git command that is used frequently
git stash show -p Displays the differences between the branch & stashed command A Git command
stash@{<number>} changes
<text> User defined input in the Git command