2

I'm quite new to using Git.

I'm working on a repository where I tried to refactor the code and failed so I ended up going back 1 commit on the master branch and worked instead on where I was at before. I've now made changes that I want to keep and do not want to merge with the commit ahead. The commit ahead is already on a remote repository as well.

What would be the solution that wouldn't disturb the commits on both my local and remote repositories whilst keeping the changes I made and not merging?

Here is a git log for clarification. The HEAD, master commit is the one I just tried to do but wouldn't let me. the origin/master is the one that is the the tip of the branch and also on Github (I think.)

* bb1d222 (HEAD, master) | * 832e8af (origin/master) |/ * 3293d35 
4
  • use git -push command and git -add <file> command Commented Dec 28, 2014 at 0:11
  • @fadfad Just to be clear: does commit 832e8af correspond to your failed attempt at refactoring the code? Also, are you the only contributor to the repo? Commented Dec 28, 2014 at 0:45
  • @Jubobs Yes and Yes. Commented Dec 28, 2014 at 0:48
  • @Jubobs Thank you for your help and patience! I followed your answer. Commented Dec 28, 2014 at 1:20

3 Answers 3

1

Based on your question and the follow-up comments, my understanding is that your repo's (recent) history looks as follows,

* bb1d222 (HEAD, master) | * 832e8af (origin/master) |/ * 3293d35 

where commit 832e8af corresponds to your failed attempt at refactoring the code.

Under the assumption that

  1. you are the only contributor to the GitHub repository;
  2. nobody else has ever cloned or forked the GitHub repository (there is a way to find out whether that's the case); and
  3. you would be happy to pretend that commit 832e8af never existed and you would like to land in the following situation

    * bb1d222 (HEAD, master, origin/master) | * 3293d35 

Then (and only then), you can simply do a force push

git push --force origin master 

Be aware that a force push is generally considered to be bad, like crossing the streams. Make sure that's really what you want to do; such an operation is complicated to reverse and has the potential to piss off your contributors (if any).

In most cases, it's preferable to revert the problematic commit (with git revert), as this, contrary to a force push, does not delete history and does not require a non-fastforward push.

Sign up to request clarification or add additional context in comments.

Comments

1

1) On your local master, execute git reset HEAD~1, and then git stash. This will put the changes you made in bb1d222 in your stash.

2) Reset hard your local master branch to origin/master (git reset --hard origin/master)

3) On master, execute git revert 832e8af, which will introduce a new commit that will undo the changes you made in 832e8af.

4) git stash pop the changes you made in bb1d222 onto master, and commit them.

These steps will produce this commit graph, and will not rewrite history

* (Commit introducing changes in bb1d222) | * (Commit reverting changes made in 832e8af | * 832e8af | * 3293d35

Comments

0

You have a few options:

  1. You could create a separate local branch and commit your changes to that local branch only. I prefer this method because then your changes are safely saved to that branch (unless your machine dies or you delete the branch).

  2. Use the git stash to save your changes (your diff so to speak).

2 Comments

Preferably, I'd love for it to stay on the master branch which is where the problem is happening.
I'm not sure I understand your situation. The remote version of the master branch is at commit 3. Your local version of the master branch is at commit 2 and you want to save your new changes to the local version of the master branch at commit 2? Do you want to completely discard the commit 3 on the remote master branch? If not you will need to resolve the merge conflicts.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.