There are some useful git commands that I’ve used a lot but I can not rememeber them all.
So I’m creating a note here.

1. Edit commit message

git commit --amend

2. Delete all local merged branches

git branch --merged | egrep -v "(^\*|master|develop|release*)" | xargs git branch -d
Note:

egrep -v helps finding all branches that doesn’t match with the following pattern.

3. Use fix up to edit the old commit

git log

Find SHA1 of the commit that you want to edit.

git commit --fixup=<sha>

Commit your edit. Replace <sha> with you SHA1 you found in the previous step.

git rebase -i --autosquash HEAD~4

Rebase the whole commit. Replace HEAD~4 with the position of commit you want to edit.

Note:

Can enable autosquash using

git config --global rebase.autosquash true

4. Restore branch, commit that was editted or deleted

git reflog 

Find SHA1 of the commit that you want to restore.

git checkout -b <branch> <sha>

Create a new branch with that commit.

5. Add, edit submodule

Add submodule

git submodule add https://github.com/user/submodule.git path/to/submodule

Delete submodule

git submodule deinit path/to/submodule
git rm path/to/submodule
git config -f .gitmodules --remove-section submodule.path/to/submodule

Sometimes after deleting submodule, you want to add that submodule back again but have error:

'path/to/submodule' already exists in the index

You need to remove cache:

git rm --cached path/to/submodule

6. Add/edit/delte remote url

# Set a new remote
git remote add origin https://github.com/user/repo.git

# Verify new remote
git remote -v
origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)

# Modify remote url
git remote set-url origin https://github.com/user/new_repo.git

# Remove remote
git remote rm origin

7. Work with tags

# Delete 1 local tag
git tag -d tag

# Delete 1 remote tag
git push --delete origin tag

# Delete all local tags.  
git tag -d $(git tag -l)

# Delete all remote tags.
git push origin --delete $(git tag -l) # Pushing once should be faster than multiple times

To be continued…