Aserian
Aserian

Reputation: 1117

Git temporarily remove some code for a cleaner pull request

I'm working of of branch A which is branched from develop. I have been working on it for a while and I'm just coming to the realization that a decent chunk of this could have been checked in as a standalone change.

To ease my reviewers strain, I'd like to delete some of my code that is unnecessary, do a pull request, then re-add the code and continue working. As our pull requests can take some time, I'd like to do it in a way that allows me to continue working with the changes intended in the first pull request, as the new code does rely on that.

I am not very familiar with rebase but it sounds like the tool for the job. My thought was this:

Update changes form develop, create a new branch from A and rebase it to develop

git pull origin develop
git checkout -b B A
git rebase develop

At this point I'd delete my code changes in B and do a pull request on it. Then I'd checkout A and rebase it onto develop in order to continue working on it.

git checkout A
git rebase B

Once B is merged into develop, I'd rebase A back onto develop

git checkout A
git rebase develop

Is there something that I'm missing about rebase that would not make this do what I expected? I'd rather do things the git way instead of copying all the files out, delete them from the PR and copying them back.

Thanks!

Upvotes: 0

Views: 319

Answers (1)

Code-Apprentice
Code-Apprentice

Reputation: 83517

I'd like to delete some of my code that is unnecessary, do a pull request, then re-add the code and continue working.

There are at least two ways to go about this:

  1. On your current feature branch, delete the code you want to get rid of and make a new commit. Then create a new branch to add the code back and continue with development from there. This is by far the most straightforward. But it adds extra commits to your history. You would do something like this:

    $ git checkout A
    # delete the code
    $ git commit -am 'Delete code'
    

    Now to restore the code on a separate branch:

    $ git checkout -b B
    $ git revert HEAD~
    
  2. Modify your branch history to remove or rearrange the history of the changes. This is way more complicated but gives a nice history where you can tell a story of how you developed each feature. The exact commands depend on exactly which commits contain the history that you want to get rid of. Most likely you will want to use git rebase -i.

    There are several details and caveats to be aware of. First, your examples using git rebase don't really accomplish very much. Presumably, branch A is already ahead of develop, so git checkout -b B A and then git rebase develop creates a copy of all the commits on branch A. The only differences will be some timestamps on each commit. This is undesirable because you should only commit a particular code change in one commit. Here you will have pairs of commits with the exact same code changes. Also, when you finally merge, you will have lots of conflicts to resolve.

    More importantly, when you rebase, everyone else that pulled your branch previously will need to delete their local copy of the branch and git fetch to see your modified history. If multiple people push to the same branch, rebasing can cause all kinds of headaches, including the aforementioned merge conflicts.

With all that said, #1 is probably the easiest way to proceed in this particular case. git rebase is a powerful tool and I suggest you read more about it.

Upvotes: 1

Related Questions