0% found this document useful (0 votes)
94 views

Git Cheatsheet

Git commands Cheat Sheet provides concise summaries of common Git commands in 3 sentences or less: Git init creates a new repository in a directory. Git add stages changes to files in preparation for committing. Git commit saves changes to the local repository and creates a new commit with the given commit message.

Uploaded by

dASAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Git Cheatsheet

Git commands Cheat Sheet provides concise summaries of common Git commands in 3 sentences or less: Git init creates a new repository in a directory. Git add stages changes to files in preparation for committing. Git commit saves changes to the local repository and creates a new commit with the given commit message.

Uploaded by

dASAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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 messag​e"]


Git init
Commits changed tracked files
git init
* Style guide for writing commit messages: http:/​/ud​aci​ty.g​it​hub.io​/gi​t-
Creates a new repository in a directory
s​tyl​eguide/
Keep commits small. Make one commit per logical change.
Git clone Messages written in present tense.

git clone [url] [new directory name]


https:​//w​ww.a​tl​ass​ian.co​m/g​it/​tut​ori​als​/sa​vin​g-c​han​ges​#gi​t-c​ommit
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 [direc​tory]

Stages changes of files in a directory git clean

https:​//w​ww.a​tl​ass​ian.co​m/g​it/​tut​ori​als​/sa​vin​g-c​han​ges​#gi​t-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 direct​ories. Use in combin​ation with previous
Resets files in working directory to be the same as the commit specified commands above

- Command works on untr​acked files (not added to staging area yet)


Git commit
- Hard filesystem deletion
git commit - Works on files, not direct​ories

Opens atom, so you can add a commit message on top line. Remember
to save https:​//w​ww.a​tl​ass​ian.co​m/g​it/​tut​ori​als​/un​doi​ng-​cha​nge​s/g​it-​clean

git commit -m ["commit messag​e"]

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 COLLAB​ORATING 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 reposi​tories. Excep​tion - if you have
git commit [commit ID] --no-e​dit
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 altern​ative
https:​//w​ww.a​tl​ass​ian.co​m/g​it/​tut​ori​als​/un​doi​ng-​cha​nge​s/g​it-​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 messag​e]*
Rename a remote repo
Edit the commit message on last commit

git commit --amend --no-e​dit 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:​//w​ww.a​tl​ass​ian.co​m/g​it/​tut​ori​als​/sy​nci​ng#​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:​//w​ww.a​tl​ass​ian.co​m/g​it/​tut​ori​als​/re​wri​tin​g-h​istory 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 repres​ented 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:​//w​ww.a​tl​ass​ian.co​m/g​it/​tut​ori​als​/sy​nci​ng#​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 repres​ent​ation of branches, including commits
Altern​ative is git fetch git log --graph --onel​ine
git pull [remote repo]/​[branch name] Conde​nsed visual repres​ent​ation 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 unnece​ssary 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
automa​tic​ally, instead of merge:
git config --global branch.au​tos​etu​prebase always git log -p [file/​dir​ect​ory]

Displays change history of file or directory


https:​//w​ww.a​tl​ass​ian.co​m/g​it/​tut​ori​als​/sy​nci​ng#​git​-pull
git log --auth​or=​[na​me]

git push Filter by author name. Show only their commits

git log --auth​or=​"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 --auth​or=​"​[person 1]\|[p​erson 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 --afte​r="[​dat​e]"

- Tags are not automa​tically pushed with other git push commands Display commits made after a certain date

git log --befo​re=​"​[da​te]​"


https:​//w​ww.a​tl​ass​ian.co​m/g​it/​tut​ori​als​/sy​nci​ng#​git​-push
Display commits made before a certain date

git log --afte​r="[​dat​e]" --befo​re=​"​[da​te]​"


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 --bran​che​s=*

Alphab​etical 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:​//w​ww.a​tl​ass​ian.co​m/g​it/​tut​ori​als​/gi​t-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:​//w​ww.a​tl​ass​ian.co​m/g​it/​tut​ori​als​/us​ing​-br​anches


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 [filen​ame]

Use with unstaged changes. Restore file in working directory to how it is at


the last commit

https:​//w​ww.a​tl​ass​ian.co​m/g​it/​tut​ori​als​/us​ing​-br​anc​hes​/gi​t-c​heckout

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 indepe​ndent lines of develo​pment, 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:​//w​ww.a​tl​ass​ian.co​m/g​it/​tut​ori​als​/us​ing​-br​anc​hes​/gi​t-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

You might also like