Branching & UndoingLearn branch management, creating, switching, merging branches, and undoing changes with git reset, git revert, and git stash to keep your repo clean.
ByAnis Mer_

Branching & Undoing

1. Introduction

If you've spent any time working with Git, you know that branches are where the magic happens. They let you create a separate space to experiment, build new features, or fix bugs without touching the main codebase.

Getting the hang of creating, switching, and deleting branches is key to keeping your work organized and your workflow efficient.

But let’s face it — mistakes are inevitable. Maybe you committed too soon, need to undo a change, or just want a clean slate. Git’s got your back with tools like git reset, git revert, and git stash.

These commands might sound intimidating at first, but once you get the hang of them, they’re lifesavers. They can help you fix mistakes, recover lost work, and keep your project history clean.

In this guide, we’ll break down branch management and show you how to master undoing changes like a pro. By the end, you’ll have the confidence to tackle whatever Git throws your way and keep your repo tidy.

2. Branch Management

Branches are essential for collaborative development, feature isolation, and efficient version control. Let’s explore how to create, switch, merge, delete, and rename branches.

1. Creating a Branch
To create a new branch, use git branch <branch-name> command. This creates a branch based on your current position in the repository.

Example:

git branch feature-x  # Creates a branch called 'feature-x'

2. Switching Branches
To start working on a different branch, use git checkout <branch-name> or the newer git switch <branch-name> command.

Examples:

git checkout feature-x  # Switches to the 'feature-x' branch
git switch feature-x    # A simpler way to switch to 'feature-x'

3. Merging Branches
When you’re ready to combine your branch’s work with another, such as merging a feature branch into main, use git merge.

Example:

git checkout main       # Switch to the main branch
git merge feature-x     # Merge 'feature-x' into 'main'

4. Deleting Branches
After a branch has served its purpose, delete it to keep your repository clean. Use git branch -d <branch-name> to remove it locally, and git push origin --delete <branch-name> to remove it from the remote.

Examples:

git branch -d feature-x  # Deletes 'feature-x' locally if it’s merged
git branch -D feature-x  # Forcefully deletes 'feature-x', even if not merged
git push origin --delete feature-x  # Deletes 'feature-x' from the remote

5. Renaming a Branch
Renaming is simple with git branch -m <old-name> <new-name>.

Example:

git branch -m old-feature new-feature  # Renames 'old-feature' to 'new-feature'

Updating Remote Tracking:

git push origin --delete old-feature   # Remove old branch from remote
git push origin new-feature            # Push renamed branch to remote

3. Undoing Changes

Mistakes happen, and sometimes you need to reverse changes or get back to a previous state. Here’s how to use git reset, git revert, and git stash to handle these situations.

1. git reset
git reset moves your branch pointer to a previous commit. It can be used to keep changes in your working directory or discard them entirely.

Examples:

  • Soft Reset: Moves the branch pointer but keeps changes staged.
    git reset --soft HEAD~1  # Moves back one commit, keeps changes staged
    
  • Hard Reset: Discards changes in the working directory and staging area.
    git reset --hard HEAD~1  # Moves back one commit, discards changes
    

2. git revert
Unlike git reset, git revert creates a new commit that undoes changes from a previous commit, preserving history.

Example:

git revert abc1234  # Creates a new commit that reverts the changes in commit abc1234

3. git stash
git stash temporarily stores uncommitted changes so you can switch branches or focus on other tasks.

Examples:

  • Stash Changes:
    git stash  # Stashes current changes
    
  • Apply Stashed Changes:
    git stash apply  # Applies the most recent stash without removing it from the stash list
    
  • Pop Stashed Changes:
    git stash pop  # Applies the most recent stash and removes it from the stash list
    

Note: git stash pop brings back your unsaved changes to the working directory and removes them from the stash list. However, it doesn't commit those changes; you'll need to commit them manually if needed.

So, when I say git stash pop applies the most recent stash, I mean it pulls those saved changes back into your working directory. It’s like picking up exactly where you left off with all your unsaved changes — both staged and unstaged — restored, so you can keep working as if you never stashed them.

Strategy Tip: Use git stash when you need to quickly switch tasks without losing your progress. Once ready, use git stash pop to restore your changes and continue where you left off.

Now, let's explore the difference between git stash apply and git stash pop in more detail.

The main difference between git stash apply and git stash pop is what happens to the stash after it’s applied:

  • git stash apply: This command restores the most recent stash (or a specific stash if specified) to your working directory but keeps the stash in the stash list. It's useful when you want to reapply changes but still keep them saved for future use.

  • git stash pop: This command also restores the most recent stash to your working directory but removes that stash from the stash list after it's applied. It’s like applying and cleaning up the stash in one step.

So, if you want to keep a copy of the stash for potential reuse, use git stash apply. If you're done with the stash and don’t need it anymore, git stash pop is the way to go.

4. git clean
To remove untracked files, use git clean. This is helpful for clearing out build files or other temporary data.

Examples:

git clean -n    # Preview files that will be deleted
git clean -f    # Force delete untracked files

5. Recovering Lost Commits with git reflog
git reflog tracks all changes made to your HEAD. If you lose a commit or move your branch by mistake, git reflog can help recover it.

Example:

git reflog  # Shows where HEAD has been
git checkout <commit-hash>  # Restore a lost commit

These branch management and undoing strategies are essential for navigating complex Git workflows. Mastering them will help you stay organized, correct mistakes, and keep your project history clean.

Stay tuned for Part 3: Advanced Git Tips, where we’ll explore more powerful commands and productivity techniques!

Back