Skip to content
Author: ytianle

Git Basic Commands II⚓︎

git reset⚓︎

git reset is a powerful command that can be used to undo changes in your Git repository. It allows you to move the HEAD pointer and optionally modify the index and working directory. Here are some common use parameters:

Let's say we have current commit history as below, and the changes introduced by commit D are the ones we want to undo:

#>>> A - B - C - D (HEAD)
#>>> Staging Area: changes from D (staged)
#>>> Working Directory: no extra changes

terms mentioned below
  • index: also known as staging area, where you can prepare changes before committing them.
  • working directory: the actual files on your filesystem that you are working with.
  • HEAD~1: a pointer that indicates the commit before the current commit.
  1. --soft: Moves HEAD only; index and working directory keep their current contents.
    • Implementation: Undoing a commit while keeping your changes staged.
    • Example:
      git reset --soft HEAD~1 # HEAD~1 can be replaced with commit hash
      
      #>>> A - B - C (HEAD)
      #>>> Staging Area: changes in D
      #>>> Unstaged Changes: empty
      #>>> Working Directory: changes in D
      
  2. --mixed: Moves HEAD and resets the index to match, but working directory keeps its current contents.
    • This is the default behavior if no option is specified.
    • Implementation: Unstaging changes from the last commit while keeping them in your working directory.
    • Example:
      git reset --mixed HEAD~1
      
      #>>> A - B - C (HEAD)
      #>>> Staging Area: empty
      #>>> Unstaged Changes: changes in D
      #>>> Working Directory: changes in D
      
  3. --hard: Moves HEAD and resets both the index and working directory to match the specified commit. All changes in the working directory are discarded.
    • Implementation: Completely undoing the last commit and discarding all changes. Really useful, if you want your branch revert back to any of the previous status.
    • Example:
      git reset --hard HEAD~1
      
      #>>> A - B - C (HEAD)
      #>>> Staging Area: empty
      #>>> Unstaged Changes: empty
      #>>> Working Directory: empty
      
  4. --keep: Moves HEAD to the specified commit, index to match the commit, but keeps changes in the working directory that do not conflict with the changes in the target commit.
    • Implementation: Undoing a commit while preserving non-conflicting changes in the working directory.
    • Example:
      git reset --keep HEAD~1
      
      #>>> A - B - C (HEAD)
      #>>> Staging Area: empty
      #>>> Unstaged Changes: changes in D
      #>>> Working Directory: changes in D
      
  5. --merge: Moves HEAD to the specified commit, resets the index to match the commit, and updates the working directory to reflect the changes, but only for files that do not have local modifications.
    • Implementation: Undoing a commit while preserving local modifications in the working directory.
    • Example:
      git reset --merge ORIG_HEAD
      
      #>>> A - B - C (HEAD)
      #>>> Staging Area: empty
      #>>> Unstaged Changes: changes in D
      #>>> Working Directory: changes in D
      

git revert⚓︎

git revert is a command used to create a new commit that undoes the changes introduced by a previous commit.

Unlike git reset, which can alter the commit history, git revert preserves the history by adding a new commit that negates the changes of the specified commit.

Implementation: If you want to undo the changes introduced by commit D without modifying the commit history, you can use git revert:

git revert <commit_hash_of_D>

#>>> A - B - C - D - E (HEAD)
#>>> E will contain the opposite changes of D, effectively undoing D's changes while keeping the history intact.

git restore⚓︎

git restore is a command used to discard changes in the working directory or to unstage changes from the staging area. It is a more intuitive way to undo changes without affecting the commit history.

# Discard changes in the working directory
git restore <file>

# Unstage changes from the staging area
git restore --staged <file>

git clean⚓︎

git clean is a command used to remove untracked files from the working directory. It is useful for cleaning up your working directory by removing files that are not being tracked by Git.

# Remove untracked files
git clean -f

# Remove untracked directories
git clean -fd # corresponding sub folders/files will be removed

# Perform a dry run to see what would be removed
git clean -n

git config⚓︎

git config is a command used to view and set Git configuration options. It allows you to customize various aspects of Git's behavior, such as user information, editor preferences, and more.

Git Configurations check⚓︎

Three common configurations to check are: user.name, user.email, and listing all configurations.

git config user.name
#>>> Your Name

git config user.email
#>>> your.email@example.com

git config --list
#>>> credential.helper=osxkeychain
#>>> init.defaultBranch=main
#>>> user.name=Your Name
#>>> user.email=your.email@example.com
#>>> core.excludesfile=/Users/yourusername/.gitignore_global
#>>> ...

git config by defualt

By default, git config uses the --local option, which means it will check the configuration for the current git repository. If you want to check the global configuration, check the following section.

Git Configurations check for global and system⚓︎

You can also check your global Git configurations by using the --global flag. This global settings will be the default for all repositories on your machine unless overridden by local repository settings.

terminal
git config --global user.email
git config --global user.name
#>>> Your Name
#>>> your.email@example.com

Find configuration source directory⚓︎

You can also add --show-origin flag to see the directory where each configuration is defined:

git config --list --show-origin
git config user.name --show-origin
git config user.email --show-origin

Update Git Configurations⚓︎

You can setup or update your Git user name and email:

terminal
git config --local/--global/--system user.name "New Name"
git config --local/--global/--system user.email "new.email@example.com"

.Config file⚓︎

Git configurations are stored in different files based on their scope:

  • Local: .git/config within the repository.
  • Global: ~/.gitconfig or ~/.config/git/config
  • System: /etc/gitconfig (requires admin access)