Merging, Stashing and Reverting in Git

Merging, Stashing and Reverting in Git

Introduction

Building on the basics of Git, this post dives into three advanced commands: git rebase, git stash, and git reset. These commands enhance your Git toolkit, offering more sophisticated ways to manage and manipulate your project’s history and state.

Understanding Advanced Git Commands

While basic Git commands are sufficient for daily operations, advanced commands like rebase, stash, and reset offer more control and flexibility, especially in complex workflows.

1. git rebase

What is git rebase?

git rebase is a command that allows you to integrate changes from one branch into another. It’s an alternative to the git merge command.

Usage and Benefits

  • Usage: git rebase [base branch]
  • Benefits:
    • Keeps a clean, linear project history.
    • Useful for cleaning up commits before merging them into a main branch.

How it Works

  • When you rebase a branch onto another, Git takes the new commits from the base branch and reapplies the commits from your current branch on top of them.
  • This process can result in conflicts, which you’ll need to resolve manually.

Best Practices

  • Avoid rebasing branches that are public and shared with others, as it can alter history in a way that is confusing for collaborators.

2. git stash

What is git stash?

git stash temporarily shelves (or stashes) changes you’ve made to your working copy so you can work on something else, and then return and re-apply them later.

Usage and Benefits

  • Usage: git stash to stash your changes and git stash pop to reapply them.
  • Benefits:
    • Allows you to switch branches without committing incomplete work.
    • Useful for quickly switching contexts without losing progress.

How it Works

  • git stash takes your uncommitted changes (both staged and unstaged), saves them for later use, and then reverts them from your working directory.

Best Practices

  • Regularly clear out your stash to avoid a buildup of stale changes.
  • Use descriptive messages with your stashes for easy identification, using git stash save "[message]".

3. git reset

What is git reset?

git reset is a powerful command used to undo local changes to the state of a Git repo.

Usage and Benefits

  • Usage: git reset [commit]
  • Benefits:
    • Allows you to undo changes in your staging area and working directory.
    • Can be used to amend the commit history.

How it Works

  • There are three main modes: --soft, --mixed (default), and --hard.
    • --soft resets the HEAD to another commit but leaves the staging area and working directory unchanged.
    • --mixed resets the HEAD and the staging area but not the working directory.
    • --hard resets everything – HEAD, staging area, and working directory, to the state of a specified commit.

Best Practices

  • Be cautious with git reset --hard as it can permanently delete your work.
  • Use git reset to clean up local history before making a pull request.

Conclusion

Understanding and correctly utilizing git rebase, git stash, and git reset can significantly improve your efficiency and workflow in Git. These commands offer more nuanced control over your repository, allowing for cleaner histories, better work-in-progress management, and undoing changes. As with any powerful tool, use them wisely and understand their impact, especially in a collaborative environment.

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.