Fundamentals 9 min read

Master Essential Git Commands: Inspect, Undo, Clean, and Speed Up Your Workflow

Learn the most useful Git commands for inspecting changes, undoing commits, cleaning untracked files, organizing history, configuring editors, and creating shortcuts, with clear examples, parameter explanations, and Bash alias setups to boost productivity for developers, data scientists, and product managers.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Essential Git Commands: Inspect, Undo, Clean, and Speed Up Your Workflow

Inspecting Changes

Use the following commands to view modifications in your repository: git diff [file] – Shows changes for all files or a specific file. git log – Displays the commit history; add -p my_file to see changes for a file. Press q to quit. git blame my_file – Shows who changed each line of a file and when. git reflog – Lists the local HEAD changes, useful for recovering lost work.

Undoing Changes

Git provides three primary commands for reverting modifications: git reset – Can undo changes in the index and working tree. Use --hard HEAD to discard all local changes since the last commit. git checkout – Restores files or commits in the working directory without affecting remote history. git revert – Creates a new commit that undoes a previous commit, safe for shared branches.

Examples: git reset --hard HEAD – Discards all local modifications. git checkout my_commit – Reverts the working tree to a specific commit. git revert my_commit – Generates a new commit that reverses my_commit.

Cleaning Untracked Files

To remove files that are not tracked by Git: git clean -n – Shows what would be deleted (dry run). git clean -f – Actually deletes untracked files. git clean -d – Deletes untracked directories as well.

By default, files listed in .gitignore are not removed, but this behavior can be changed.

Organizing Commits and Tags

git commit --amend

– Adds staged changes to the most recent commit or edits its message (only if the commit hasn't been pushed). git push --tags – Sends all local tags to the remote repository, useful for version releases.

Tools like bump2version can automate tag creation for Python packages.

Changing the Default Editor

Replace Vim with another editor (e.g., Atom) for Git commit messages:

git config --global core.editor "atom --wait"

Creating Shortcut Aliases

Add convenient aliases to .bash_profile (or .zshrc) to shorten common 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 '

If the profile file does not exist, create it on macOS:

touch ~/.bash_profile
open ~/.bash_profile

These aliases behave exactly like the full Git commands, saving keystrokes and speeding up daily workflows.

Conclusion

The guide covers essential Git commands for inspection, undoing, cleaning, organizing, editor configuration, and shortcut creation, providing practical examples and parameter details that help developers, data scientists, and product managers work more efficiently with version control.

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-lineversion controlgit revertgit cleanbash alias
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.