10 Useful Git Commands You Always Need

image from https://git-scm.com

1.Show non-pushed commits

git log origin/master..HEAD

You can also see the differences with diff command

git diff origin/master..HEAD

2.Modify the most recent commit

git commit --amend

3.Undo latest commit

Preserves local changes:

git reset --soft HEAD~1

Deletes local changes. (Be careful!)

git reset --hard HEAD~1

If you want to undo n commits use HEAD~n

4. Create a patch from changes

git diff > patch1.patch

Create patch for all non-pushed commits

git format-patch origin/master

This command creates a patch for each commits. Here is the output:

0001-commit-1.patch
0002-commit-2.patch

5. Apply a patch

git apply < patch1.patch

6.Revert local changes

Revert tracked changes:

git checkout .

Remove untracked files and folders:

git clean -fd

7.Merge with rebase (master is forward, merge master into dev)

git checkout dev
git rebase master

8.Unite multiple commits into a single commit

You have a few commits for a single feature. For a clean commit history you want to unite the into a single commit. The command below unites the latest 2 commits into one.

git rebase -i HEAD~2

You will see a terminal editor (Vim) with 2 commits

pick 3c041de commit 1
pick c0203cb commit 2

# Rebase 85cf1c2..c0203cb onto 85cf1c2 (2 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
...

Press “i” to edit interactively. Then change second commit’s “pick” to “f”

pick 3c041de commit 1
f c0203cb commit 2

# Rebase 85cf1c2..c0203cb onto 85cf1c2 (2 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
...

Save and exit (esc then :wq then hit enter)

9. Delete local commits

Assume that you have 3 local commits. But you don’t want to push them all. You need to delete some of them.

git rebase -i HEAD~3

You will see the last 3 commits with “pick”

pick 3c041de commit 1
pick 10bfb27 commit 2
pick 69ac547 commit 3

Just delete the lines you don’t want to push by double tapping “d”. I deleted “commit 2” and “commit 3”

pick 3c041de commit 1

Save and exit editor.

10. Delete remote commits

Follow the commands in the previous step. Then use force push command.

Think twice before using force push. Because this will overwrite your remote commits. You may mess up everything.
git push -f origin dev

You can use force push if you want to change (rebase etc. ) your remote commits. If you haven’t pushed your commits yet. You don’t need force push.

If you liked the article, click the 💚 below so more people can see it! Also, you can follow me on Medium