DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Git cheat sheet for daily use
Rashwan Lazkani
Rashwan Lazkani

Posted on • Updated on

Git cheat sheet for daily use

Here is a collection of useful Git commands that I use and that could be helpful to you when working with Git in your daily work.

Git Reference
A git reference can be found here.

Setup
This is the name that will be associated when a commit is made in the version history

git config --global user.name β€œ<firstname lastname>”
Enter fullscreen mode Exit fullscreen mode

Set an email address which will be associated with your user

git config --global user.email β€œ<valid-email>”
Enter fullscreen mode Exit fullscreen mode

Add Git

git init
git add .
git status
git remote add origin "URL"
git commit -m "Initial commit"
git status
git push -u origin master
Enter fullscreen mode Exit fullscreen mode

Commit

git status
git add .
git commit -m "COMMIT MESSAGE"
git status
git push -u origin master
Enter fullscreen mode Exit fullscreen mode

Commit with Body and Subject

git commit -m "this is the subject" -m "this is the body"
Enter fullscreen mode Exit fullscreen mode

Clone

git clone <URL>
Enter fullscreen mode Exit fullscreen mode

Pull
Fetch and merge any commits from the tracking remote branch

git pull
Enter fullscreen mode Exit fullscreen mode

Branch
Check which branch is checked out

git push -u origin <branch>
Enter fullscreen mode Exit fullscreen mode

Create new feature branch

git checkout -b feature/test
Enter fullscreen mode Exit fullscreen mode

Force create new branch

git checkout -b feature/test
Enter fullscreen mode Exit fullscreen mode

Push local branch

git push -u origin <branch>
Enter fullscreen mode Exit fullscreen mode

Checkout branch

git checkout <branch name>
Enter fullscreen mode Exit fullscreen mode

Log
Show all commits in the current branch’s history

git log
Enter fullscreen mode Exit fullscreen mode

Show all commits in the current branch’s history in one line

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Tree in Terminal

git log --graph --oneline --all
Enter fullscreen mode Exit fullscreen mode

Diff

git diff
Enter fullscreen mode Exit fullscreen mode

NOTE: a good tip is that you can always use the following buttons to exit or git help in the git diff and git log screen for example:
Type q to exit the screen. Type h to get help.


Reset to commit

git reset --hard <commit id>
Enter fullscreen mode Exit fullscreen mode

Stop track a folder or file

git rm --cached <file>/<folder>
git commit -m "Removed file that shouldn't be tracked"
Enter fullscreen mode Exit fullscreen mode

Stash

Save un-committed changes

git stash
Enter fullscreen mode Exit fullscreen mode

List Stashes

git stash list
Enter fullscreen mode Exit fullscreen mode

Delete all stashes

git stash clear
Enter fullscreen mode Exit fullscreen mode

Drop specific Stash

git stash drop stash@<stash id>
Enter fullscreen mode Exit fullscreen mode

View Stash changes

git stash show
Enter fullscreen mode Exit fullscreen mode

View Stash full diff

git stash show -p
Enter fullscreen mode Exit fullscreen mode

Merge the specified branch into the current one

git merge <branch>
Enter fullscreen mode Exit fullscreen mode

Rebase
Apply any commits of current branch ahead of specified one

git rebase <branch>
Enter fullscreen mode Exit fullscreen mode

Clear staging area, rewrite working tree from specified commit

git reset --hard <commit>
Enter fullscreen mode Exit fullscreen mode

Cherry Pick
Run git log --oneline to get a log of your commits history. The commit hash is what we need to start the cherry picking.

Checkout the branch where you want to cherry pick the specific commit/commits.

Now we can cherry pick from one branch into our master branch.

To cherry pick one commit - this will cherry pick the commit with the selected and add it as a new commit on the master branch. It will have a new (and different) commit ID in the master branch than the branch it was originally pushed into.

git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

To cherry pick more than one commit

git cherry-pick <commit-hash> <commit-hash>
Enter fullscreen mode Exit fullscreen mode

If the cherry picking gets halted because of conflicts, resolve them by

git cherry-pick --continue
Enter fullscreen mode Exit fullscreen mode

If you want to abort this step

git cherry-pick --abort
Enter fullscreen mode Exit fullscreen mode

You can also cherry pick a merge

git cherry-pick -m 1 <hash>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Join us at DEV
Yes, this is technically an β€œad”, but really we just want to ask if you want to join DEV. We have 900k+ developers reading, posting, and enjoying community, and would love to have you. Β  Create an account and continue your coding journey.