JS_VIPER
JS_VIPER

Reputation: 141

Not erasing the previous commits and changes to it.

Hey I am new to github so please help me out.

I have four commits.

commit4 eh87eg87

commit3 e3e983jj

commit2 de6d6e3d

commit1 du3qw6y1

I want to go back to commit2 and make changes to it and commit it (commit 5) and not lose the rest of the commits like commit4, commit3, commit2 and commit1.

Upvotes: 1

Views: 50

Answers (3)

rupert thurner
rupert thurner

Reputation: 1

you might try to

git revert --no-commit de6d6e3d
... hack ...
git commit -m "re commit2"

if you found the best solution, you might want to add it here: https://git.wiki.kernel.org/index.php/GitSvnClient_(git-svn)

Upvotes: 0

Sergiu Dumitriu
Sergiu Dumitriu

Reputation: 11601

You can try two things:

  1. git rebase --interactive <ID of the commit before commit1>. Read the documentation that git prints for you. Basically, you have to use edit instead of pick for commit1, amend your commit, then continue the rebase.
  2. Note down the IDs of the commits, git reset --hard <ID of the commit before commit1>, then take each commit in order and copy them using git cherry-pick <ID of commit>, change the files that you want to change then git commit --ammend, and go to the next one.

Upvotes: 1

Nevik Rehnel
Nevik Rehnel

Reputation: 51945

You can just create another branch, which will serve as an identifier for your "dangling" commits, and then reset your current branch to commit 2:

  1. git branch temp_branch (you can choose any name here)
  2. git reset --hard de6d6e3d (the SHA of commit 2)

After creating the temporary branch in step 1, you will still be on your original branch. Once you reset that, you can continue working upon commit 2, and if you need the other two commits, just checkout temp_branch (or use it for rebase, cherry-pick, etc.)

Upvotes: 0

Related Questions