This document lists some of the common commands used for Git
.
git log
git shortlog
git log --oneline --no-merges
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
git lg
git clone https://github.com/ashwanikumar04/git-cheatsheet.git
git clone [email protected]:ashwanikumar04/git-cheatsheet.git
Sometimes when the repository is very large, we only want to clone a single branch. Use below command to clone a single branch
git clone [email protected]:ashwanikumar04/git-cheatsheet.git -b master --single-branch
The following command makes the output of git commands colorful.
git config --global color.ui true
git config user.name "Ashwani Kumar"
git config user.email "[email protected]"
git config --global user.name "Ashwani Kumar"
git config --global user.email "[email protected]"
git config --global alias.st status
Just type git st
whenever we want to see the status of the repository
git config --global alias.co checkout
Just type git co
whenever we want to check out a branch
git config --global alias.st status
Just type git ci
whenever we want run a commit
git status
Shows the status with tracked and un-tracked files
git add .
Adds all the files in the current directory to staging.
git add -A
Adds all files in the current repository (even new files that are not yet tracked)
git add -u
Add all files that are already being tracked (ignore new files)
git reset --soft HEAD^
Undo last commit of entire repo, but leave files staged.
git reset --hard HEAD^
Completely blow away last commit. Changes files to state of previous commit.
git reset --hard HEAD^^
Completely blow away last two commits. Changes files to state of previous commit.
git reset --hard HEAD^^^
Completely blow away last three commits. Changes files to state prior to last third commit.
Returns files to state they were in, after specified commit
git reset --hard <sha-of-commit>
git push origin HEAD --force
git commit -m "Add file to repository"
Commit staged files to the repository
git commit --amend -m "New Message"
Changes the commit message for the last commit
git commit -am "New Message"
Lets us add and commit all tracked, modified files in one step.
git checkout -b <name_of_branch>
Create a branch and check it out in one step
git branch <name_of_branch>
Create a branch, but stay in current branch.
git checkout <name_of_branch>
Check out an already created branch
git branch
See a list of all branches, highlights the currently checked out branch
git checkout master
Checkout master branch
git branch -d <name_of_branch>
Remove branch locally, but only if we have merged branch.
git branch -D <name_of_branch>
Remove branch even if we haven't merged changes.
git stash
Use this to stash all the changes to the current repository
git stash apply
Use this to apply the last stash
git push --set-upstream origin <branch>
git push
git pull origin/master