Fundamentals 9 min read

Master Essential Git Commands: Inspection, Reverting, Cleanup, and Shortcuts

This guide walks developers through the most useful Git commands for inspecting changes, safely reverting commits, cleaning untracked files, customizing the default editor, and creating handy Bash aliases, providing clear examples, code snippets, and practical tips to streamline version‑control workflows.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Master Essential Git Commands: Inspection, Reverting, Cleanup, and Shortcuts

Inspecting Changes

Common commands for reviewing repository state include: git diff – Show changes in all local files; add a filename to limit output to that file. git log – Display the commit history. Use git log -p <file> to see patches for a specific file; press q to quit the pager. git blame <file> – Show which author last modified each line of a file and when. git reflog – List the local HEAD reference log, useful for locating lost commits.

Reverting Commits

Git provides three primary ways to undo changes: git reset – Moves the HEAD pointer and optionally updates the index and working tree. git reset --hard HEAD discards all modifications since the last commit. git checkout – Restores files or an entire commit to the working tree without creating a new commit. It is safe for local‑only rollbacks because it does not rewrite shared history. git revert <commit> – Creates a new commit that inverses the changes introduced by <commit>. This preserves the existing history and is the recommended method for undoing commits that have already been pushed to a shared branch.

Use git reset or git checkout for local, unpushed work. For collaborative branches, prefer git revert to avoid rewriting history.

Cleaning Untracked Files

To delete files that are not tracked by Git: git clean -n – Dry‑run; shows what would be removed without deleting anything. git clean -f – Actually delete untracked files. git clean -d – Delete untracked directories as well.

Combine flags (e.g., git clean -fd) to delete both files and directories in one step.

By default, patterns listed in .gitignore are respected; use -x to also remove ignored files if needed.

Changing the Default Editor

Git uses Vim by default for commit messages. To configure a different editor (for example, Atom), run: git config --global core.editor "atom --wait" After installing the chosen editor, Git will launch it for all interactive prompts.

Creating Git Command Shortcuts

Define Bash aliases in ~/.bash_profile (or ~/.bashrc) to shorten frequently used Git commands:

alias gs='git status '
alias ga='git add '
alias gaa='git add -A '
alias gb='git branch '
alias gc='git commit '
alias gcm='git commit -m '
alias go='git checkout '

After adding the lines, reload the profile with source ~/.bash_profile (or start a new terminal session). The aliases behave exactly like the full commands.

If the profile file does not exist, create it on macOS with: touch ~/.bash_profile Then open it for editing, for example with open ~/.bash_profile.

Escaping Vim When Stuck

If a Vim session opens unintentionally (e.g., during a commit), exit safely using these four steps:

Press i to enter insert mode.

Type your commit message on the first line (or make any needed edits).

Press Esc to leave insert mode.

Type :x and press Enter to save and quit.

Additional Resources

Official documentation and interactive tutorials for deeper learning:

Atlassian Bitbucket Git guide:

https://www.atlassian.com/git/tutorials/learn-git-with-bitbucket-cloud

Interactive branching tutorial:

https://learngitbranching.js.org/
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GitCommand-lineVimversion controlgit revertgit cleanbash aliasgit config
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.