Frage

I did some wrong rebasing and conflict resolution in Git, so I ended up in this stupid situation:

*   8379515 (HEAD, origin/master, origin/HEAD, master) Merge branch 'binary-mobile'
|\
| * 21b247a (binary-mobile) Add Binary Mobile
| * c66dced Add Music Collection
* | cbbe364 Add task Music Collection
|/
* 8e9ccae Update README.md
* 31d2050 Reorganize repo and add README files
* 88fe4fd Rename readme file
* 8ed72bd Reorganize the structure of the repo
* 219a25a Update README.md
* 60921e6 Create README.md
* 1de4f36 Initial commit

My problem is that commits Add task Music Collection and Add Music Collection are the same. I want to:

  • have my branch follow a single path;
  • only have one commit with the title Add Music Collection.

Could I just do git rebase -i 8e9ccae and then squash c66dced into cbbe364 and rename cbbe364 to Add Music Collection? I think that the problem in the first place was that I had pushed the commits I wanted to rebase to Github, but I thought that as long as nobody else is working with them, this is ok?

War es hilfreich?

Lösung 2

I would do something like:

  • go back to the point where the commits are ok
  • create a branch there, and cherry-pick the commits you want
  • git reset to the starting-point of the branch, this will become your new master/head, but you saved your inbetween commits
  • merge branch and done! :)

So in code that would be something like:

git checkout 8e9ccae
git checkout -b better_changes
git cherry-pick c66dced
git cherry-pick 21b247a
git checkout master
git reset --hard HEAD^6  //check correct nr of steps with git reflog
git merge better_changes

And then force push to origin :)

Andere Tipps

I would do the following if I would be stuck with this problem:

1.) Rebase the commits between cbbe364 (exclusive) and 21b247a (inclusive) onto your 8e9ccae.

git rebase --onto 8e9ccae cbbe364 21b247a

This moves the commits c66dced and 21b247a directly onto 8e9ccae. This will result in another hash value for the newest commit Add Binary Mobile, let's call the hash value 'commit2' (Tree may not be 100% accurate, have no git repository to reproduce it)

*   8379515 (HEAD, origin/master, origin/HEAD, master) Merge branch 'binary-mobile'
|\
| * 21b247a (binary-mobile) Add Binary Mobile
| * c66dced Add Music Collection
| | cbbe364 Add task Music Collection
|/
|
|
| * commit2 (HEAD) Add Binary Mobile
| * commit1 Add Music Collection
|/
* 8e9ccae Update README.md
* 31d2050 Reorganize repo and add README files
* 88fe4fd Rename readme file
* 8ed72bd Reorganize the structure of the repo
* 219a25a Update README.md
* 60921e6 Create README.md
* 1de4f36 Initial commit

2.) After that move your master down (down the 'Tree') to commit2:

git branch -d master
git checkout commit2
git branch master

| * commit2 (HEAD, master) Add Binary Mobile
| * commit1 Add Music Collection
|/
* 8e9ccae Update README.md

3.) Now you have your master where it belongs, namely on commit2. If you want to follow your branch on a 'single line', you need to get rid of binary-mobile branch, just delete it:

git branch -D binary-mobile

Because the commit 21b247a now has no branch name, and you do not work on this branch (no HEAD) the branch will disappear.

4.) Finally push your changes (force is necessary in this case). This will move your origin/master down to master, and the merge branch should disappear.

git push master --force


* commit2 (HEAD, master, origin/master) Add Binary Mobile
* commit1 Add Music Collection
* 8e9ccae Update README.md
* 31d2050 Reorganize repo and add README files
* 88fe4fd Rename readme file
* 8ed72bd Reorganize the structure of the repo
* 219a25a Update README.md
* 60921e6 Create README.md
* 1de4f36 Initial commit

If I understand you correctly you need to simply check and undo what you did wrong.

With git reflog (See here) you can check history of everything you made with your git repository. All these changes are stored in git, so you can go back to any state your repository had for previous 2 months.

With git reset --hard HEAD@{NumberOfTheChange} (See here) you can undo any changes in the repository. (But please be aware that this way you can't undo changes in the files, which was not tracked by git)

After this you can do what you want.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top