Useful Commands

Table of contents
  1. Configuration
    1. See current configuration
    2. Global user config
    3. Local user config
    4. Command alias
    5. Global ignore
  2. Untrack file
  3. Fix previous commit
  4. Rebase
  5. Cherry Pick
  6. Remove or modify a commit with rebase
  7. Reset status
    1. To remove all uncommitted changes
    2. Undo last (few) commit(s) without losing changes
  8. Prune remote branches

Configuration

See current configuration

git config --list

Global user config

git config --global user.name "myname"
git config --global user.email "myemail@example.com"

Local user config

Useful when you need to use different identity per project,

git config --local user.name "anothername"
git config --local user.email "anotheremail@example.com"

Command alias

If you’re tired of writing long git commands that you frequently use,

git config --global alias.youralias "command to shorten (without 'git')"

Global ignore

First create a ~/.gitignore_global file. Then,

git config --global core.excludeFile ~/.gitignore_global

Untrack file

When you have a file that you would like to untrack,

git rm -r --cached <file-or-dir>

Fix previous commit

This comes in handy when you make a small change in your code after you’ve committed, but you realize you probably wanted it included in your last commit.

# Modify commit message as well
git commit --amend
# Keep the commit message
git commit --amend --no-edit

If you already pushed the commit to a remote before the ammend, then you need to force push the new changes by git push -f origin master.


Rebase

To rebase from a remote,

git fetch
git rebase origin/main

Or simply

git pull --rebase

Cherry Pick

To cherry pick a single commit from anothe branch, first check the hash of the commit. Then,

git cherry-pick <hash>

Remove or modify a commit with rebase

To modify a commit from the past, first check the number of commits you must go back to find the commit you want to modify. Then,

git rebase -i HEAD~<num-commits-to-target-inclusive>

Then you can choose an action for each commit by editing the text editor which looks like:

pick <hash> A Commit Message4
pick <hash> A Commit Message3
pick <hash> A Commit Message2
pick <hash> A Commit Message1

Modify pick to a command listed in the editor comments.


Reset status

There are 5 types of reset: --soft, --mixed, --hard, --merge, and --keep.

Each type of reset has different effects on the index, working tree, and HEAD.

The first three are the most commonly used, and --mixed is the default.

  • All of them move HEAD to the specified commit.
  • --soft leaves changes in the staging area.
  • --mixed keeps the changes in an unstaged state.
  • --hard discards all changes.

To remove all uncommitted changes

Changes reset by following commands will be unrecoverable unless you have already pushed to remote.

git reset --hard  # Reset tracked files to your latest commit
git clean -fd     # Remove untracked files

Undo last (few) commit(s) without losing changes

git reset HEAD^   # Undo last commit
git reset HEAD^^  # Undo last two commits
git reset HEAD~3  # Undo last three commits

Prune remote branches

If you have deleted a remote branch, but it still shows up when you run git branch -a, then you can prune it by,

git remote prune $remote_name  # --dry-run to see what will be pruned

If you have a local branch that is still tracking the deleted remote, then you need to delete the local branch first.


References: