Git branch question.

Status
Not open for further replies.
Hey all, i have a (hopefully) quick and easy git question that i would like help on.

Let's give a simple timeline of events.

Yesterday i did a standard pull, and got my local 'develop' branch in line with 'remotes/origin/develop'. I then proceeded to make about 10 commits from that point on. None of these commits have been pushed to the remote.

I would like to take all 10 of those commits and move them over to a different branch (which we'll call 'temp work' for the time being), such that locally 'develop' now points back to where i started from, and 'temp work' points to the last of the 10 commits i made.

i.e. i want to be in the same state i would have been had i branched from develop before starting the work, and then made those 10 changes in the new branch.

I want to do this so i cna push the changes to hte server (paranoia in case my computer dies) without affecting the server's 'develop' branch itself.

Thanks!
 
Note: i *think* i coudl do this with:

Code:
git branch temp-work
git checkout develop
git reset --hard HEAD~10
git checkout temp-work

However, i'm not certain if that will work, and i'm always a bit wary of doing something new in git that i haven't done before. The part i'm most wary about is the 'reset' operation. There are a lot of args that it can take and i don't want to screw it up.

Alternatively, i think i could do:

Code:
git branch temp-work
git checkout develop
git reset --hard <sha id of the commit i want my 'develop' to be back at>
git checkout temp-work

But, again, i would like someone with strong git-fu to weigh in.

Thanks!
 

Jonathon

Ars Legatus Legionis
17,646
Subscriptor
[url=http:/civis/viewtopic.php?p=26729499#p26729499:17x25nie said:
Metasyntactic[/url]":17x25nie]
Alternatively, i think i could do:

Code:
git branch temp-work
git checkout develop
git reset --hard <sha id of the commit i want my 'develop' to be back at>
git checkout temp-work

But, again, i would like someone with strong git-fu to weigh in.

Thanks!
This is what I'd do. The version with the relative reference (HEAD~10) is functionally equivalent assuming you're sure they point to the same commit, but I like being explicit, especially if it's more than a couple revs away (that way, I guarantee that I didn't miscount or anything like that).

Alternatively, you could do what btmlltt suggested and delete your local copy of develop... probably a bit easier, but the end result should be the same. (Also, possibly marginally less safe, as 'git branch -d <branchname>' also deletes the branch's reflog... which is your safety net here in case you deleted something you didn't mean to.)
 

MilleniX

Ars Tribunus Angusticlavius
8,141
Subscriptor++
To be both more conservative and explicit in what you're doing, I would do it as follows:
Code:
git checkout -b temp-work
git push origin temp-work
git checkout develop
git reset --keep origin/develop
That way you get your changes backed up to the server before you even attempt the reset, and the reset is explicitly anchored to a particular ref on the server, rather than having to count or cross-reference explicitly.

Also, thanks Catfish for teaching me git reset --keep. That's a new one to me, compared to manually running git status before git reset --hard.
 

xav0989

Seniorius Lurkius
22
[url=http:/civis/viewtopic.php?p=26736029#p26736029:2pu6y1ec said:
MilleniX[/url]":2pu6y1ec]To be both more conservative and explicit in what you're doing, I would do it as follows:
Code:
git checkout -b temp-work
git push origin temp-work
git checkout develop
git reset --keep origin/develop
That way you get your changes backed up to the server before you even attempt the reset, and the reset is explicitly anchored to a particular ref on the server, rather than having to count or cross-reference explicitly.

You don't really need to do more than
Code:
git branch temp-work
git reset --keep origin/develop

Since the reflog will still contain pointers to all the commits you've made.
 

Jonathon

Ars Legatus Legionis
17,646
Subscriptor
[url=http:/civis/viewtopic.php?p=26748799#p26748799:1qg8khmn said:
MilleniX[/url]":1qg8khmn]The reflog is also automatically expired and garbage collected over time. I approached this as deterministically operating so that the risk of data loss fell with each step, even if the user made mistakes or took other steps in the interim.
And given that the desired end result is "temp-work gets pushed to the server"... might as well actually push it. ;)
 
Status
Not open for further replies.