Git commands Cheat Sheet
by itsellej via cheatography.com/55812/cs/14830/
SETTING UP A REPOSITORY Git commit
git commit
Opens atom, so you can add a commit message on top line.
Git init
Remember to save
git init
git commit -m ["commit message"]
Creates a new repository in a directory
Add commit message using the command line
git commit -a -m ["commit message"]
Git clone
Commits changed tracked files
git clone [url] [new directory name]
* Style guide for writing commit messages: http://udacity.github.io/git-
Clone a repo into a new directory
styleguide/
git clone [url]
Keep commits small. Make one commit per logical change.
Clone a repo into the current directory Messages written in present tense.
SAVING CHANGES https://www.atlassian.com/git/tutorials/saving-changes#git-commit
Git diff
Git add
git diff
git add [file name] Display changes to files in working directory (not staged)
Add files to staging area git diff --staged
git add . Display changes to staged files
Add all changed files to staging area **git diff [commit id 1] [commit id 2]
git add '*[file type]' Compare two commits
Example "git add *.txt" to add only text files to the staging area git diff HEAD
git add [directory] Display changes between staged and unstaged file changes
Stages changes of files in a directory
Compare changes between files
https://www.atlassian.com/git/tutorials/saving-changes#git-add
UNDOING CHANGES
Git reset
git reset HEAD [file name]
git clean
Resets file in working directory to be the same as the HEAD (last)
commit git clean -n
git reset [commit ID] Dry run. Does not delete files, but shows which files would be
deleted
Resets files in working directory to be the same as the commit
specified git clean -f
Initiates the actual deletion of untracked files
git clean -d
By itsellej Published 5th March, 2018. Sponsored by Readable.com
cheatography.com/itsellej/ Last updated 5th March, 2018. Measure your website readability!
Page 1 of 5. https://readable.com
Git commands Cheat Sheet
by itsellej via cheatography.com/55812/cs/14830/
git clean (cont) COLLABORATING AND SYNCING - GITHUB
Remove any untracked directories. Use in combination with previous
commands above
Git remote
- Command works on untracked files (not added to staging area yet)
- Hard filesystem deletion git remote
- Works on files, not directories Check if you have any remote repositories. Exception - if you have
cloned a repo, command will return original repo as a remote repo
https://www.atlassian.com/git/tutorials/undoing-changes/git-clean git remote -v
Displays the full path to the remote repo
git revert
git remote add origin [github url]
git commit HEAD
Add a remote repo. Origin = name of remote repo. Can add altern‐
Reverses most recent commit
ative name instead of origin
git commit [commit ID]
git remote [url] [branch name]
Reverses changes made associated with a specific commit ID
Point remote branch to correct url
git commit [commit ID] --no-edit
git remote rm [remote repo name]
Will not open the editor. Default command will open editor
Remove connection to remote repo specified
- Inverts changes made from the previous commit git remote rename [remote repo name] [new name]
- History of commits is not lost
Rename a remote repo
- Good for shared repos
When you have multiple branches, you can:
https://www.atlassian.com/git/tutorials/undoing-changes/git-revert - merge all branches into your local repo, and push to remote repo,
or;
REWRITING HISTORY - push individual branches from local to remote repo
https://www.atlassian.com/git/tutorials/syncing#git-remote
git commit --amend
Git fetch
git commit --amend m [new commit message] *
git fetch [remote repo name]
Edit the commit message on last commit
Retrieve all branches from remote repo
git commit --amend --no-edit
git fetch [remote repo name] [branch]
Adding forgotten staged files to recent commit with no commit
Retrieve all commits on remote's (origin) master branch*. Use when
message
both local and remote have changes the other does not have
git commit --amend
git fetch --dry-run
Take most recent commit and add new staged changes to it
- Run when nothing is staged*
- Amended commits are new commits. Previous commit will no
longer be available
- Don't use on public commits which other devs have based their
work on
https://www.atlassian.com/git/tutorials/rewriting-history
By itsellej Published 5th March, 2018. Sponsored by Readable.com
cheatography.com/itsellej/ Last updated 5th March, 2018. Measure your website readability!
Page 2 of 5. https://readable.com
Git commands Cheat Sheet
by itsellej via cheatography.com/55812/cs/14830/
Git fetch (cont) INSPECTING A REPOSITORY
See changes to the remote repo before pulling into local repo
- Use to see what everybody else has been working on
Git shortlog & git log
- Fetched content is represented as a remote branch. Does not affect
local repo git shortlog
- Follow with git merge origin/master to merge remote repo changes Alphabetical list of names and commit messages made by each
to local repo person
- Then push new merge commit back to the remote repo git shortlog -s -n
- git push origin master
Displays the number of commits made next to each person's name
git log
https://www.atlassian.com/git/tutorials/syncing#git-fetch
Shows all commits made. Full history
Git pull git log — stat
git pull [remote repo] Displays names of files changed during the commits
Pull changes from remote repo to your local repo. Fast forward git log --graph
merge. Alternative is git fetch Visual representation of branches, including commits
git pull [remote repo]/[branch name] git log --graph --oneline
Pull changes from remote repo branch to your local repo Condensed visual representation of branches, including commits
git pull --rebase [remote repo] * git log -n [number]
Pull and merge remote into local Displays specified number of commits only
- To be used if remote repo may have changes in the form of merged git log -p [commit id]
commits
Displays changes made to the file(s)
- Git pull command = git fetch and git merge
git log -patch [commit id]
- using rebase ensures a linear history by preventing unnecessary
merge commits Displays changes made to the file(s)
- can use following command to ensure git pull uses rebase automa‐ git log -p -w
tically, instead of merge: Ignores whitespace changes
git config --global branch.autosetuprebase always
git log -p [file/directory]
Displays change history of file or directory
https://www.atlassian.com/git/tutorials/syncing#git-pull
git log --author=[name]
git push Filter by author name. Show only their commits
git push [remote repo] [branch name] git log --author="full name"
Push commits from local repo to remote repo. Example: git push Filter by author's full name. Show only their commits
origin master git log --author="[person 1]\|[person 2]"
git push [remote repo] --all Show commits by either person 1 or person 2
Push commits from all local branches to remote repo git log --grep="Search term"
git push [remote repo] --tags * Show commits which contain the search term only in the commit
Sends all of your local tags to the remote repository message
- Tags are not automatically pushed with other git push commands git log --after="[date]"
Display commits made after a certain date
https://www.atlassian.com/git/tutorials/syncing#git-push
By itsellej Published 5th March, 2018. Sponsored by Readable.com
cheatography.com/itsellej/ Last updated 5th March, 2018. Measure your website readability!
Page 3 of 5. https://readable.com
Git commands Cheat Sheet
by itsellej via cheatography.com/55812/cs/14830/
Git shortlog & git log (cont) USING BRANCHES
git log --before="[date]"
Display commits made before a certain date
Git branch
git log --after="[date]" --before="[date]"
git branch
Display commits made after but before a certain date
List of branches in repository
git log -- [file name 1] [file name 2]
git branch [new branch name]
Display history related to file or files
Creates a new branch
git log --branches= *
git branch [new branch name] [commit id]
View commits across all branches
Creates a new branch and points it to the commit specified
Displays list of commits made.
git branch -d [branch name]
- Down arrow scrolls through commit history.
- Press q to exit. Deletes a branch. Use -D to force delete
- date format = yy-m-d git branch -m [new name]
Rename an existing branch
https://www.atlassian.com/git/tutorials/git-log
git branch -a
List all remote branches
Git status
https://www.atlassian.com/git/tutorials/using-branches
git status
List which files are staged, unstaged, and untracked.
Git checkout
Git show git checkout [branch name]
Switch to working on another branch
git show
git checkout -b [new branch name]
Display changes made in the last commit
Create a new branch and switch to it
git show [commit id]
git checkout [commit id]
Display changes made in a specific commit
Viewing how files were when the commit was created
git show HEAD
git checkout HEAD [filename]
Show details of the commit HEAD is currently pointing at
Use with unstaged changes. Restore file in working directory to how
it is at the last commit
https://www.atlassian.com/git/tutorials/using-branches/git-checkout
Git merge
git merge [branch name]
By itsellej Published 5th March, 2018. Sponsored by Readable.com
cheatography.com/itsellej/ Last updated 5th March, 2018. Measure your website readability!
Page 4 of 5. https://readable.com
Git commands Cheat Sheet
by itsellej via cheatography.com/55812/cs/14830/
Git merge (cont)
[Branch name] is name of branch that will be merged into receiving
branch (where HEAD is currently pointing to
- Integrate independent lines of development, created by git branch,
and integrate them into a single branch
- use git status to ensure HEAD is pointing to merge receiving
branch
- use git fetch to ensure all branches are up to date with remote
changes
https://www.atlassian.com/git/tutorials/using-branches/git-merge
OTHER
Git tag
git tag
Displays all current tags
git tag -a [new tag name]
Create a new tag at current commit
git tag -a [new tag name] [7 digits of commit id]
Create a new tag at a previous commit
git tag -d [tag name]
Delete a tag
- Purpose: to point out particular commits / make them stand out
- Example: label with a version number
- Tag stays locked to a commit
git rebase
git rebase -i HEAD~[num]
Merge a number [num] of commits*. Creates a new commit id
*HEAD points to the current location
By itsellej Published 5th March, 2018. Sponsored by Readable.com
cheatography.com/itsellej/ Last updated 5th March, 2018. Measure your website readability!
Page 5 of 5. https://readable.com