Maving a commit forward (git)

Over the past few days I’ve been trying to learn more about git and Sourcetree, and I’ve been trying to apply this to my current Battle Tank project.
Now everything has gone wrong…


My original idea was to create a separate feature/ branch for the two Turret Rotation challenge videos. But somehow I rebased (maybe also cherry-picked, can’t remember) and now I have this mess.
What I want to get back to is: having a blue master branch and having a checked out pink feature/TurretRotation with the commit BT228 Challenge - Turret Rotation pt1 (v1).
Is there a way to get back to this? If not … what other options do I have?

Thanks in advance!
-Enrico

I’ve made this progress (if I can call it that) by deleting the origin/feature/TurretRotation branch. Is this a good practice or should I avoid doing this in the future?


Also, I still need help with getting to what I described above. I think the first thing to do is to get rid of the rebased commits and move them back to their original place, but how can I do this?
Thanks again!

Update #1:
I have merged the the commit BT228 Challenge - Turret Rotation pt1 (v1) onto the master branch.
Is this better or worse than before?

I’m not 100% sure what you’re trying to end up with

@DanM, I’d like to end up with this:


(sorry for my bad photoshop skills…)
In words, what I think I want to do is: undo my rebase of commits BT230 ... and BT Using Clamp() to Limit Values - AimingComponent not showing up in Tank_BP so they end up where they originally where (like in the picture above). Then I would like to create a branch called TurretRotation with the commit BT228 as the latest commit.

UPDATE:
I think I have made progress:


Now I’d just like to delete the current commit and go back to having BT228 Challange - Turret Rotation pt1 (v1) as my last commit. Is this possible? I’m having a hard time using

git reset --hard commit-hash

to delete this commit. How could I go about moving the master branch first, and then deleting the commit?

It’s commit-hash not name. You can get that by right clicking and using Copy SHA

@DanM that’s what I meant to say. I didn’t know the right terminology at the time.


Also, I’ve made progress and I’m almost near the end.
My last question is how can I move the commit BT228 Challenge - Turret Rotation pt1 (v1) to be my latest commit?
I’ve tried using an interactive rebase to reorder commits, but it doesn’t seem to be working in this case. Any other options?

You could make a patch of that commit, drop it with rebase, then apply it.

Thanks @DanM! But I’m not really sure which commands I should use… Could you explain it a bit better?
Thanks in advance!

Sure, let me test this first though :sweat_smile:

Actually, what problems did you have with interactive rebase?

Test #1

I get some strange behavior when doing a normal interactive rebase. Or this behaviour could be completely normal, but since this is my first time rebasing I’m not really sure what’s supposed to happen.
Here’s what I did:

  1. I use git rebase -i HEAD~3 to get to this
  2. The commit BT Using Clamp() to Limit Values - AimingComponent not showing up in Tank_BP (4f861ad) is my latest commit (excluding the merge). So I reorder the commits like this:
  3. Following the guidance from this website.
  4. This happens:

    Which I don’t think this is what I want, but I could very well be wrong.

Test #2

I’ve also tried making an empty commit and then reordering, so that I don’t copy the commit BT Using Clamp() to Limit Values - AimingComponent not showing up in Tank_BP over and over again.
But this happens:

  1. Create an empty commit using $ git commit --allow-empty -m "Placeholder commit", which does this:
  2. Then I do another interactive rebase (like above):
  3. But then this happens

Test #3

One reason I was thinking this is acting strangely is that you usually rebase from one branch onto your master branch. So I’ve also tried putting my BT228 Challange ... commit onto a feature/TurretRotation branch and then rebasing, but in that case the commit doesn’t even show up.

  1. New branch
  2. Rebasing using git rebase master (no need for interactive stuff this time)
  3. But that only moves the branch and not the commit:

So, what can I try from here?
And sorry for the big post…

If I’m understanding what you’re after Test #1 is what you want with the only difference with the latest commit being on a different branch.

So you from there you would just need to create a new branch from that commit, switch back to master and hard reset to the previous commit, then switch to the new branch.

Is this what you mean, @DanM?
If so, how will the origin/ branches connect back up?

You will need to force push as you’ve modified the history. Be sure you want to do that before you do that though. Take backups if you want to be extra careful (just clone your remote in a different folder).

Ok, great! So I just need to do this to create a backup of my remote repo, correct?
youmeanthis

That’s what I’d do. (Also I don’t want to be the cause of f’ing up your whole repo :sweat_smile:)

@DanM, no problem! Thanks for all the help.
I’ve never forced pushed. Is this the command I would use?

git push -f

Although, I’ve seen some people say the lower one is better

git push origin feature/TurretRotation --force

Or should I do something more like this?
Also, if I wanted to replace my local repo with the .zip I just downloaded, what would I need to do?

Well the latter one is more explicit about what you’re overwriting though I believe you want to force push master as your new branch isn’t on your remote. With lease I’ve never used so :man_shrugging:

I did not realise that what you are highlighting. I was suggesting cloning

git clone URL BackupBT

Then you would just need to force push that one.

Ok done the clone. But no change happened … is that normal?

Also, I’m not sure what you mean by “that one.” Do I need to push the clone or the original?

Ok I made some assumptions I probably shouldn’t have.

The plan for the backup was to make a brand new local repo by cloning what you currently have on your remote. If it does get messed up, you can force push that fresh clone.

What you did was clone it into your current repo so now you will have

Your current in

~/Documents/gamedev/gamedevUnreal/repos/02_BattleTank/

and then the fresh clone in (a git repo inside another)

~/Documents/gamedev/gamedevUnreal/repos/02_BattleTank/backupBT

I was suggesting you put that in, for example,

~/Documents/gamedev/gamedevUnreal/repos/backupBT

You should be able to just cut and paste that directory.


You need to force push the 02_BattleTank one. If things go wrong you can restore everything by force pushing the clean backupBT