DEV Community

Khoa Pham
Khoa Pham

Posted on • Edited on

Useful git commands for everyday use!

Do you know that questions about git get the most views on StackOverflow? I’ve searched a lot on Google how to execute certain actions with git, and this actually slowed me down a lot. There are some actions that we tend to use a lot, so it’s good to learn them. Here are my favorites, learning from friends and internet, hope you find them useful.

Before we begin, you should run git --version to check your current git version, mine is 2.12.2 as in macOS High Sierra. Here is the official git documentation, you can read details about git commands, parameters and new releases of git.

Table of Contents

Useful commands

🔍 Status

Check the status of working directory and staging area:

git status
Enter fullscreen mode Exit fullscreen mode

Show changes between HEAD and working directory:

git diff
Enter fullscreen mode Exit fullscreen mode

Show the list of commits in one line format:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Show commits that make add or remove a certain string:

git log -S 'LoginViewController'
Enter fullscreen mode Exit fullscreen mode

Search commits that contain a log message:

git log — all — grep=’day of week’
Enter fullscreen mode Exit fullscreen mode

🔍 Tag

List all tags:

git tag
Enter fullscreen mode Exit fullscreen mode

Tag a commit:

git tag -a 1.4 -m "my version 1.4"
Enter fullscreen mode Exit fullscreen mode

Delete remote tags:

git push --delete origin tagname

git push origin :tagname
Enter fullscreen mode Exit fullscreen mode

Push tag to remote:

git push origin tagname
Enter fullscreen mode Exit fullscreen mode

Rename tag:

git tag new old
git tag -d old
git push origin :refs/tags/old
git push --tags
Enter fullscreen mode Exit fullscreen mode

Move tag from one commit to another commit:

git push origin :refs/tags/<tagname>
git tag -fa tagname
git push origin master --tags
Enter fullscreen mode Exit fullscreen mode

🔍 Remote

List all remote:

git remote
Enter fullscreen mode Exit fullscreen mode

Rename remote:

git remote rename old new
Enter fullscreen mode Exit fullscreen mode

Remove stale remote tracking branches:

git remote prune origin
Enter fullscreen mode Exit fullscreen mode

🔍 Branch

List all branches:

git branch
Enter fullscreen mode Exit fullscreen mode

Create the branch on your local machine and switch in this branch:

git checkout -b branch_name
Enter fullscreen mode Exit fullscreen mode

Create branch from commit:

git branch branch_name sha1_of_commit
Enter fullscreen mode Exit fullscreen mode

Push the branch to remote:

git push origin branch_name
Enter fullscreen mode Exit fullscreen mode

Rename other branch:

git branch -m old new
Enter fullscreen mode Exit fullscreen mode

Rename current branch:

git branch -m new
Enter fullscreen mode Exit fullscreen mode

Rename remote branch:

git branch -m old new               # Rename branch locally    
git push origin :old                 # Delete the old branch    
git push --set-upstream origin new   # Push the new branch, set local branch to track the new remote
Enter fullscreen mode Exit fullscreen mode

Delete a branch:

git branch -D the_local_branch

git push origin :the_remote_branch
Enter fullscreen mode Exit fullscreen mode

🔍 Commit

Undo last commit:

git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

Squash last n commits into one commit:

git rebase -i HEAD~5

git reset --soft HEAD~5
git add .
git commit -m "Update"
git push -f origin master
Enter fullscreen mode Exit fullscreen mode

Move last commits into new branch:

git branch newbranch
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.*1
git checkout newbranch
Enter fullscreen mode Exit fullscreen mode

🔍 Cherry Pick

Add some commits to the top of the current branch:

git cherry-pick hash_commit_A hash_commit_B
Enter fullscreen mode Exit fullscreen mode

🔍 Reflog

Show reflog:

git reflog
Enter fullscreen mode Exit fullscreen mode

Get commit:

git reset --hard 0254ea7

git cherry-pick 12944d8
Enter fullscreen mode Exit fullscreen mode

🔍 Revert

Revert the previous commit:

git revert HEAD
git commit
Enter fullscreen mode Exit fullscreen mode

Revert the changes from previous 3 commits without making commit:

git revert --no-commit HEAD~3..
Enter fullscreen mode Exit fullscreen mode

🔍 Amend

Amend previous commit:

git commit --amend

git commit --amend --no-edit

git commit --amend -m "New commit message"
Enter fullscreen mode Exit fullscreen mode

Changing git commit message after push:

git commit --amend -m "New commit message"
git push --force <repository> <branch>
Enter fullscreen mode Exit fullscreen mode

🔍 Checkout

Checkout a tag:

git checkout tagname

git checkout -b newbranchname tagname
Enter fullscreen mode Exit fullscreen mode

Checkout a branch:

git checkout destination_branch
Enter fullscreen mode Exit fullscreen mode

Use -m if there is merge conflict:

git checkout -m master // from feature branch to master
Enter fullscreen mode Exit fullscreen mode

Checkout a commit:

git checkout commit_hash

git checkout -b newbranchname HEAD~4

git checkout -b newbranchname commit_hash

git checkout commit_hash file
Enter fullscreen mode Exit fullscreen mode

Checkout a file:

git checkout c5f567 -- Relative/Path/To/File
Enter fullscreen mode Exit fullscreen mode

🔍 Stash

Save a change to stash:

git stash save "stash name"

git stash
Enter fullscreen mode Exit fullscreen mode

List all stashes:

git stash list
Enter fullscreen mode Exit fullscreen mode

Apply a stash:

git stash pop

git stash apply

git stash apply stash@{2}
Enter fullscreen mode Exit fullscreen mode

🔍 Rebase

Rebase the current branch onto master:

git rebase master // rebase the current branch onto master
Enter fullscreen mode Exit fullscreen mode

Continue rebase:

git rebase --continue
Enter fullscreen mode Exit fullscreen mode

Abort rebase:

git rebase --abort
Enter fullscreen mode Exit fullscreen mode

🔍 .gitignore

Un-track files that have just been declared in .gitignore:

git rm -r --cached .
git add .
git commit -am "Remove ignored files"
Enter fullscreen mode Exit fullscreen mode

🔍 Index

Remove untracked files:

git clean
Enter fullscreen mode Exit fullscreen mode

Remove file from index:

git reset file
Enter fullscreen mode Exit fullscreen mode

Reset the index to match the most recent commit:

git reset
Enter fullscreen mode Exit fullscreen mode

Reset the index and the working directory to match the most recent commit:

git reset --hard
Enter fullscreen mode Exit fullscreen mode

🔍 Misc

Get their changes during git rebase:

git checkout --ours foo/bar.java
git add foo/bar.java
Enter fullscreen mode Exit fullscreen mode

Get their changes during git merge:

git pull -X theirs

git checkout --theirs path/to/the/conflicted_file.php

git checkout --theirs .
git add .

git checkout branchA
git merge -X theirs branchB
Enter fullscreen mode Exit fullscreen mode

Merge commits from master into feature branch:

git checkout feature1
git merge --no-ff master
Enter fullscreen mode Exit fullscreen mode

Find bug in commit history in a binary search tree style:

git bisect start

git bisect good

git bisect bad
Enter fullscreen mode Exit fullscreen mode

Git alias

If there are commands that you use a lot, then consider using git alias. This is how to make alias for git status, then you can just type git st:

git config — global alias.st status
Enter fullscreen mode Exit fullscreen mode

Alias configurations are stored in .gitconfig file, you can learn some cool aliases from thoughtbot and mathiasbynens.

GUI clients

Doing things in command line is cool and faster. However for viewing branches and commits, I find using a GUI client more visualizing and comfortable. You can see a list of all GUI clients here, I myself use SourceTree.

Check before you commit

We usually have some experiment code that we don’t want they to step into our commit. I usually mark my experiment with // but sometimes forget to unstage that.

Starting with 2.9, Git has improvement on its commit hook which makes it globally using hooksPath.

Firstly we nee to create a file called pre-commit, and place it into, for example, /Users/khoa/hooks:

In your project, run git config core.hooksPath /Users/khoa/hooks.

Whenever you commit a file with that pattern, it won’t let you commit. For how to make this work in SourceTree, check:
SourceTree and pre commit hook
*Pre-commit file works perfectly in terminal, but SourceTree seems to ignore it. I use both terminal and SourceTree, as…*medium.com

Where to go from here

This is just scratching the surface of what git can do, if you want to learn more, here are some links to get started.

Original post https://medium.com/flawless-app-stories/useful-git-commands-for-everyday-use-e1a4de64037d

Top comments (0)