Git useful commands

Dimitar Danailov
2 min readSep 8, 2019

--

Photo by Yancy Min on Unsplash

Git is one of the most important development tool. I’d like to share list with useful commands:

How to delete a Git branch both locally and remotely?

git branch -d <branch_name>
git push <remote_name> --delete <branch_name>

Review / edit the last commit

git commit --amend

Set up git to pull and push all branches

git push --all <remote>

What’s the simplest way to get a list of conflicted files?

git diff --name-only --diff-filter=U

How to undo last commit(s) in Git?

git reset --hard HEAD~1

How can I undo git reset — hard HEAD~1?

git reset --hard <sha1 of desired commit>

How can I merge two commits into one?

git reset --soft "HEAD^"
git commit --amend

Cherry-Picking specific commits from another branch

# Initial statedd2e86 - 946992	- 9143a9 - a6fd86 - 5a6057 [master]
\
76cada - 62ecb4 - b886a0 [feature]
git checkout master
git cherry-pick 62ecb4
# Outputdd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 - 62ecb4 [master]
\
76cada - 62ecb4 - b886a0 [feature]

How to checkout master to all submodules (sub repositories) of project ?

# For git 1.8.2 or above the option --remote was added to support updating to latest tips of remote branches:
git submodule update --recursive --remote

# For older, git 1.6.1 or above you can use something similar to (modified to suit):
git submodule foreach git pull origin master

Is it possible to completely empty a remote Git repository?

touch README.md
git init
git commit -m "I am removing all previous commits"
git push <remote_name> <branch_name> -f

I want to share list with useful training sessions.

Thanks for reading this article. Be sure to like and recommend it if you found it helpful. For more about Git follow me so that you’ll get get notified when I write new posts, or connect with me here on Github or Linkedin.

--

--

Dimitar Danailov

Founder of #Javascript Consulting Company. Open source lover and contributor: @FirefoxDevTools . Ex Software Architect and CTO. BioHacker and fasting lover.