You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.
Changing Directory
Command
Description
cd
Change directory
cd ..
Change directory to parent directory
cd ~
Change directory to home directory
cd -
Change directory to previous directory
pwd
Print working directory
Setting up Alias
Command
Description
git config --global alias.co checkout
Set co as alias for checkout
git config --global alias.br branch
Set br as alias for branch
git config --global alias.ci commit
Set ci as alias for commit
Git Configuration
Command
Description
git config
Check all configuration options
git config --list
Check all configuration options with name and email
git clone [https://url]
Clone source code from a remote repository
git config --global user.name "Your name"
Configure username
git config --global user.email "Your email"
Configure email
git config --global core.editor vim
Configure editor
Getting & Creating Projects
Command
Description
git init
Initialize a local Git repository
git clone [https://url]
Clone source code from a remote repository
git clone [https://url] [folder]
Clone source code from a remote repository into a specific folder
git clone --bare [https://url]
Clone source code from a remote repository without a working directory
git clone --mirror [https://url]
Clone source code from a remote repository without a working directory and without the remote repository
Basic Commands
Command
Description
git status
Check status
git add [file]
Add file to staging area
git add .
Add all files to staging area
git add -A
Add all files to staging area
git add -u
Add all modified files to staging area
git add -p
Add all modified files to staging area interactively
git commit -m "message"
Commit changes
git commit -a
Commit all changes
git commit -am "message"
Commit all changes with message
git commit --amend
Amend last commit
git commit --amend -m "message"
Amend last commit with message
git commit --amend --no-edit
Amend last commit without changing the commit message
git commit --amend --reset-author
Amend last commit with new author
git commit --amend --no-edit --reset-author
Amend last commit with new author and without changing the commit message
Push changes to remote repository (and remember the branch)
git push
Push changes to remote repository (remembered branch)
git push origin --delete [branch]
Delete a remote branch
git push origin :[branch]
Delete a remote branch
git push origin [branch] --force
Force push changes to remote repository
Inspection & Comparison
Command
Description
git log
View changes
git log --summary
View changes (detailed)
git log --oneline
View changes (brief)
git log --stat
View changes (detailed)
git log --patch
View changes (detailed with actual changes)
git log --graph
View changes (graphical)
git log --graph --oneline
View changes (graphical and brief)
git log --graph --oneline --all
View changes (graphical, brief and all branches)
Undoing Things
Command
Description
git reset [file]
Unstage a file while retaining the changes in working directory
git reset --hard
Discard all local changes in your working directory
git reset --hard HEAD
Discard all local changes in your working directory
git reset --hard origin/[branch]
Discard all local changes in your working directory and get the latest version from the remote repository
git reset --hard [commit]
Discard all local changes in your working directory and get the specific commit from the remote repository
git checkout -- [file]
Discard local changes in a specific file
git checkout [branch]
Switch to a branch and discard local changes
git revert [commit]
Revert a commit
git revert [commit] --no-commit
Revert a commit without committing
Syncing Forks
Command
Description
git remote -v
List all currently configured remote repositories
git remote add upstream [https://url]
Specify a new remote upstream repository that will be synced with the fork
git fetch upstream
Fetch the branches and their respective commits from the upstream repository. Commits to master will be stored in a local branch, upstream/master
git merge upstream/master
Merge the changes from upstream/master into your local master branch. This brings your fork's master branch into sync with the upstream repository, without losing your local changes
Rewrite History
Command
Description
git rebase -i HEAD~[number]
Interactive rebase
git rebase -i [commit]
Interactive rebase
git rebase -i [branch]
Interactive rebase
git rebase -i [SHA1]
Interactive rebase
git rebase -i [tag]
Interactive rebase
Stashing
Command
Description
git stash
Stash changes in a dirty working directory away
git stash save "message"
Stash changes in a dirty working directory away with a message
git stash list
List all stashed changesets
git stash show
Show the changes in the last stashed changeset
git stash show -p
Show the changes in the last stashed changeset (detailed)
Tagging
Command
Description
git tag
List all tags
git tag -l "v1.8.5*"
List all tags matching a pattern
git tag [tag]
Annotate a tag
git tag -a [tag] -m "[message]"
Annotate a tag with a message
Common Problems Resolution
Delete All Commit
If you want to delete all commits and keep only one commit of all the commit.
#Checkoutgitcheckout--orphanlatest_branch#Add all the filesgitadd-A#Commit the changesgitcommit-am"commit message"#Delete the branchgitbranch-Dmain#Rename the current branch to maingitbranch-mmain#Force update your repositorygitpush-foriginmain
Update Commit
If you have commited and realized that there is small change needed.
#make your change then,gitadd . # or add individual filesgitcommit--amend--no-editgitcommit--amend-m"an updated commit message"# now your last commit contains that change!
Update commit message
If you want to update previous commit message.
gitcommit--amend# follow prompts to change the commit message
Undo a commit from like 5 commits ago
Turns out you don't have to track down and copy-paste the old file contents into the existing file in order to undo changes! If you committed a bug, you can undo the commit all in one go with revert.
# find the commit you need to undogitlog# use the arrow keys to scroll up and down in history# once you've found your commit, save the hashgitrevert [savedhash]
# git will create a new commit that undoes that commit# follow prompts to edit the commit message# or just save and commit