Skip to main content
  1. Blog/

Advanced Git Guide: Master History, Debugging, and Complex Workflows

·4 mins

If you already master the basic commands (add, commit, push, pull), it’s time to unlock the true power of Git. This guide is designed to solve complex scenarios, audit code, and save projects from catastrophes.

1. Surgical Commits (Partial Staging) #

Sometimes you modify many things in a single file, but you want to separate them into different commits to keep an atomic and clean history.

💡 Tip: In interactive modes, Git usually opens your default text editor (frequently Vim). To save and exit in Vim, press Esc and type :wq followed by Enter.

CommandDescription
git add -p <file>(Patch) Starts an interactive mode that asks you snippet by snippet (hunk) if you want to add it to staging.
git commit --amend --no-editAdds the current changes in staging to the last existing commit without opening the editor to change the message.
git stash push -m "msg"Creates a stash with a custom message to easily identify it in the future.
git stash -uTemporarily saves your changes, including new files that Git is not yet tracking (untracked).

2. Rewriting History (Advanced Rebase) #

Rebase is not only used to update branches; it is the ultimate tool to “makeup” your history before pushing it to the remote repository.

The Golden Rule of Rebase
Danger: Never rebase commits that you have already pushed to a public repository and that other people are using. Rewriting public history causes massive conflicts.

CommandDescription
git rebase -i <commit>Opens the history from the indicated commit to reorder, merge (squash), edit, or delete specific commits.
git cherry-pick <commit>Takes a specific commit from any branch and applies it as a new commit on your current branch1.
git rebase --onto <new-base> <old-base> <branch>Moves a branch (or a segment of commits) from one parent branch to a completely different one.
git pull --rebaseDownloads changes from the remote and applies your local commits on top of them, avoiding the automatic merge commit.

3. Debugging and Code Auditing #

When a bug appears and no one knows when it was introduced, these commands will save you hours of manual code reading.

CommandDescription
git blame <file>Shows line by line who was the last person to modify each fragment of a file and in which commit they did it.
git grep "text"Searches for a text string or regular expression throughout your tracked source code extremely fast.
git bisect startStarts a binary search assistant to find exactly which commit introduced a bug2.
git bisect good <commit>Tells bisect a point in history where you know the code worked well.
git bisect bad <commit>Tells bisect the current (or past) commit where the error is already present.

4. Parallel Workspaces (Worktrees) #

git worktree is a superpower that few people know about. It allows you to have multiple branches open simultaneously in different folders on your computer, using the same local repository.

CommandDescription
git worktree add <path> <branch>Creates a new folder on your system linked to the repository, checking out the specified branch.
git worktree listShows all the parallel working directories you have active.
git worktree remove <path>Safely deletes a parallel working directory.

5. Maintenance and Extreme Rescue #

If you deleted a branch by mistake, did a disastrous reset --hard, or Git seems to be failing, this is your first aid kit.

CommandDescription
git reflogThe absolute record. Shows all movements of HEAD, even changes discarded by a reset or deleted branches.
git fsck(File System Check) Verifies the integrity of Git’s internal database and looks for corrupt or lost objects.
git gc(Garbage Collector) Cleans up unnecessary files and optimizes the local repository, reducing its size on disk.

6. Custom Shortcuts (Aliases) #

If you get tired of typing long commands, Git allows you to create your own shortcuts by modifying your .gitconfig file.

CommandDescription
git config --global alias.st statusCreates the shortcut git st to quickly view the status of your files.
git config --global alias.co switchCreates the shortcut git co to quickly switch branches.
git config --global alias.lg "log --oneline --graph --decorate"(Highly recommended) Creates the git lg command to render the history as a compact and easy-to-read visual tree.

Notes and References #


  1. The cherry-pick command is ideal when you fix a bug in the development branch (develop) and need to bring that exact fix to the production branch (main) without merging the rest of the unfinished features. ↩︎

  2. How does git bisect work? It uses a binary search algorithm. You give it a “good” commit and a “bad” one. Git jumps right to the middle of the history between both points. You test your code and tell Git if that point is good (git bisect good) or bad (git bisect bad). Git will continue splitting the history in half until it isolates the exact commit that broke the application. To finish, use git bisect reset↩︎

Gerardo Catalas
Author
Gerardo Catalas
Focused on backend development, with hands-on experience in C# (.NET) and SQL Server. Currently expanding my skills through continuous learning and personal projects.