philosopher
philosopher

Reputation: 1151

Squash merged a branch into master on Github that was missing some changes

I am the sole developer on a project I am working on. I am using git and Github for version control.

My workflow is quite simple:

  1. Create new feature_branch from master.
  2. After the work on feature_branch is done, it is pushed upstream.
  3. Changes are reviewed and a pull request is made.
  4. Pull request is squash merged into master.

The previous commit that was squash merged into master on remote was missing a one-line comment. I could always follow the protocol outlined above and create a new local branch, push it up and merge it into master with this change. However, it seems like such a waste just for a one-line comment.

One solution I thought of was to perform the following steps:

git checkout master 
git reset HEAD~ // Revert last commit
// Add the missing comment to my code
git add .
git commit -m 'Commit message' // This would be the same commit message as the reverted commit
git push -f origin master

Questions:

  1. The solution mentioned above involves force pushing to remote master. I am worried that it might mess up the version history. Will it? Is this the best way to go about it?
  2. If not, what would be the best practise / safe method for performing such a change?

Upvotes: 0

Views: 128

Answers (1)

David Sugar
David Sugar

Reputation: 1236

A push -f can mess up version history. If you know what you are doing, it won't. The commands you are doing won't mess up history, as long as you are not out-of-date on your local repo. (Make sure to do a git fetch origin right before you git push -f).

That being said, even if you are sure you won't be messing up history, you still may not want to do this. You are creating a new commit and replacing the existing commit (even if you are only adding a one line comment). If someone has updated with your original commit, you may cause them trouble by replacing your commit with a new one.

In the case where you are working with other people and have already pushed your commits, I strongly recommend not re-writing history--instead create a new commit after your original with any fixes you need.

Upvotes: 1

Related Questions