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