Basic commands are well documented elsewhere. Here are some operations I’ve found useful in real projects.

Resources


Delete a Remote Branch

Since Git v1.7.0, the recommended syntax is:

1
2
3
4
5
# Recommended (v1.7.0+)
$ git push origin --delete <branchName>

# Legacy syntax (push nothing to the target branch)
$ git push origin :<branchName>

See git-push docs.

Undo the Last Commit

1
2
3
4
5
$ git commit ...              # (1) Commit
$ git reset --soft HEAD~1     # (2) Undo commit, keep working tree changes
# << edit files >>            # (3) Make corrections
$ git add ....                # (4) Stage changes
$ git commit -c ORIG_HEAD     # (5) Recommit with original message

Reference: Stack Overflow.

Note: If you only need to fix a commit message, use git commit --amend instead. It is simpler and preserves the commit history.

Cherry-Pick a Specific Commit

When working with multiple branches, it is easy to accidentally modify the wrong branch. Instead of manually reapplying changes:

1
$ git cherry-pick commit-hash

References