git

Synopsis

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

CommandDescription
cdChange directory
cd ..Change directory to parent directory
cd ~Change directory to home directory
cd -Change directory to previous directory
pwdPrint working directory

Setting up Alias

CommandDescription
git config --global alias.co checkoutSet co as alias for checkout
git config --global alias.br branchSet br as alias for branch
git config --global alias.ci commitSet ci as alias for commit

Git Configuration

CommandDescription
git configCheck all configuration options
git config --listCheck 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 vimConfigure editor

Getting & Creating Projects

CommandDescription
git initInitialize 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

CommandDescription
git statusCheck status
git add [file]Add file to staging area
git add .Add all files to staging area
git add -AAdd all files to staging area
git add -uAdd all modified files to staging area
git add -pAdd all modified files to staging area interactively
git commit -m "message"Commit changes
git commit -aCommit all changes
git commit -am "message"Commit all changes with message
git commit --amendAmend last commit
git commit --amend -m "message"Amend last commit with message
git commit --amend --no-editAmend last commit without changing the commit message
git commit --amend --reset-authorAmend last commit with new author
git commit --amend --no-edit --reset-authorAmend last commit with new author and without changing the commit message
git commit --amend --no-edit --date="date -R"Amend last commit with new date
git commit --amend --no-edit --date="date -R" --reset-authorAmend last commit with new date and new author
git commit --amend --no-edit --date="date -R" --reset-author --allow-emptyAmend last commit with new date and new author and allow empty commit

Branching & Merging

CommandDescription
git branchList all branches
git branch -aList all branches (local and remote)
git branch -rList all remote branches
git branch -vList all branches with last commit on each branch
git branch -vvList all branches with last commit and commit author on each branch
git branch -vvvList all branches with last commit, commit author and commit message on each branch
git checkout -b [branch]Create a new branch and switch to it
git merge [branch]Merge a branch into the active branch
git merge [source branch] [target branch]Merge a branch into a target branch
git branch -d [branch]Delete a branch
git branch -D [branch]Force delete a branch
git push origin --delete [branch]Delete a remote branch
git branch -m [old branch] [new branch]Rename a branch
git branch --set-upstream-to=origin/[branch] [branch]Set a local branch’s upstream branch
git branch --unset-upstream [branch]Unset a local branch’s upstream branch
git push origin [branch]Push a branch to your remote repository

Sharing & Updating Projects

CommandDescription
git push origin [branch]Push a branch to your remote repository
git push -u origin [branch]Push changes to remote repository (and remember the branch)
git pushPush 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] --forceForce push changes to remote repository

Inspection & Comparison

CommandDescription
git logView changes
git log --summaryView changes (detailed)
git log --onelineView changes (brief)
git log --statView changes (detailed)
git log --patchView changes (detailed with actual changes)
git log --graphView changes (graphical)
git log --graph --onelineView changes (graphical and brief)
git log --graph --oneline --allView changes (graphical, brief and all branches)

Undoing Things

CommandDescription
git reset [file]Unstage a file while retaining the changes in working directory
git reset --hardDiscard all local changes in your working directory
git reset --hard HEADDiscard 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-commitRevert a commit without committing

Syncing Forks

CommandDescription
git remote -vList 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 upstreamFetch 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/masterMerge 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

CommandDescription
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

CommandDescription
git stashStash changes in a dirty working directory away
git stash save "message"Stash changes in a dirty working directory away with a message
git stash listList all stashed changesets
git stash showShow the changes in the last stashed changeset
git stash show -pShow the changes in the last stashed changeset (detailed)

Tagging

CommandDescription
git tagList 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.

#Checkout
git checkout --orphan latest_branch
#Add all the files
git add -A
#Commit the changes
git commit -am "commit message"
#Delete the branch
git branch -D main
#Rename the current branch to main
git branch -m main
#Force update your repository
git push -f origin main

Update Commit

If you have commited and realized that there is small change needed.

#make your change then,
git add . # or add individual files
git commit --amend --no-edit

git commit --amend -m "an updated commit message"
# now your last commit contains that change!

Update commit message

If you want to update previous commit message.

git commit --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 undo
git log
# use the arrow keys to scroll up and down in history
# once you've found your commit, save the hash
git revert [saved hash]
# git will create a new commit that undoes that commit
# follow prompts to edit the commit message
# or just save and commit