Git
version control
branching
pull request
software development

moving committed but not pushed changes to a new branch after pull

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

When working with Git, a distributed version control system, developers often encounter scenarios where they have unpushed commits and then need to handle changes in their local branch after performing a git pull . In situations where a pull operation introduces new changes that conflict with the local commits, moving the committed (but not pushed) changes to a new branch may be an efficient way to resolve these conflicts. This operation allows developers to manage changes systematically, maintain code stability, and reduce potential integration issues.

Understanding Git Branches and Commits

In Git, branches are pointers to specific commits in the project's history. They facilitate multiple lines of development, enabling collaboration and parallel work streams:

  • Commits: Snapshots representing changes made to files in a repository. Each commit is trackable and reversible.
  • Branches: Allow for separate lines of work, like developing new features or fixing bugs, without interfering with the main codebase.

Scenario: Moving Committed Changes to a New Branch

Imagine you have committed changes in the feature-a branch but haven't pushed them to the remote repository. Subsequent changes from collaborators have been introduced through a pull operation, necessitating the relocation of your unpushed changes to a new branch to resolve conflicts cleanly.

To illustrate the steps, we will explain the process with relevant Git commands.

Step-by-Step Process

  1. Check Current Status: Before taking any action, it is always a good idea to check the current state of your repository.
  • Data Loss Prevention: Commands such as git reset --hard should be used with caution since they can permanently discard changes.
  • Commit Integrity: Ensure that you only cherry-pick the intended commits to maintain a clean history.
  • Branch Naming Conventions: Use clear and descriptive branch names for easy identification and collaboration.
  • Conflict Resolution: While Git does have mechanisms like merge to resolve conflicts, isolating work in branches can often result in more clear and systematic conflict resolution.
  • Reflog Usage: Using reflog efficiently allows retrieval of commit hashes that are not easily accessible otherwise, safeguarding your developmental efforts.
  • Backup before Complex Operations: Taking backups before performing operations like resets ensures you retain all changes in case something goes awry.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.