Search code examples
gitversion-controlbranchcommit

Temporarily switch to a different commit, commit again and go back to original commit


I have a branch called develop in which I am at commit c08e2887. I have to go back to an old commit 6a73b96e, make some changes to that old commit, commit again (let's call it abc) and push it to my remote branch develop. Later on, I will need to go back to my (current) commit c08e2887 both locally and remotely.

The remote branch to which I push the commit abc should have the same name develop, it cannot be a different one because I need to run some other process which depends on it.

What is the best way to do this?

Alternatively, is the following doing the job?

Starting from the current branch develop at commit c08e2887, I was thinking:

  • Create a new branch called new_version as a backup of the current state
  • In branch develop, git checkout 6a73b96e to go back to the desired commit
  • Make changes, commit and push to remote develop: now both local and remote branches at commit abc
  • Run the process that depends on remote branch develop
  • From local branch develop at commit abc, create a new branch called old_version as a backup
  • Delete local branch develop
  • From local branch new_version, create a (now new) branch develop (this will be equal to what I had before all this process)
  • Push this (new) branch develop to the remote branch develop to go back to what I had before also remotely
  • Delete local branch new_version

Solution

  • Your suggestion will get the job done, but involves quite a lot of unnecessary steps. Here's a shorter version:

    git checkout -b quickfix 6a73b96e # create a new branch from your old commit
    # make changes
    git commit -m 'these are my changes for commit abc'
    git push origin +quickfix:develop # force-push quickfix over develop
    # run whatever things you need to run in your remote repository
    git push origin +develop:develop # force-push to restore your original develop
    

    Theoretically, you don't even need to create a quickfix branch and you could simply work in "detached HEAD" state, but it doesn't really matter since the number of commands remains the same.