Frank Szczerba on 25 Oct 2012 09:53:10 -0700

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] git-fu


You can also directly create a branch at the desired point.

$ git branch branch_name head~1

If you have made several commits since the desired branch point, you can use a sha1 to identify the desired commit:

$ git log
< find the sha1 of the commit you want to base the branch on >
$ git branch branch_name commit_sha1

When you are ready to switch to working on the alternate approach, just check out the branch as normal.

If you decide to move master to the new branch, the easiest way is to just reset master. This is fine if you haven't shared the changes yet:

$ git branch -M master

If you _have_ shared the changes, then you should avoid rewriting history:

$ git checkout branch_name       # if necessary
$ git merge -s ours master       # create a fully merged commit with our tree
$ git checkout master            # switch to master
$ git merge --ff branch_name     # fast-forward to the merged commit
$ git branch -d branch_name      # clean up the working branch

As you said, there are many ways to do it.

Since branches are so cheap and easy to work with in git, you should consider doing _all_ development work on topic branches, merging into master only when it's tested and working. If you only work on one thing at a time, the merge to master will likely be a fast-forward, but you'll always have easy access to a "last known good" version of the code.


On Oct 25, 2012, at 12:13 PM, Adam <> wrote:

> Commit your changes then
> git reset --hard head~1 
> That will set you one commit back. Then
> git checkout -b branch_name
> Will put you on a new branch
> On Oct 25, 2012, at 12:04 PM, "Eric at" <> wrote:
>> Hash: SHA1
>> My limited git skills are proving to be insufficient today.
>> Background: I made some code changes and committed them.
>> I subsequently made more changes but now I'm thinking I
>> /may/ have gone down the wrong path.
>> I'd like to go back to the last commit, create a branch,
>> and try another approach in that branch.  How can I do
>> that without losing the current changes in the event I
>> want to come back to them?
>> Presumably, if the new branch works then I'd merge it into
>> master (and then I don't care about what happens to the
>> current changes.)
>> I suspect tmtowtdi.
>> Eric
>> - -- 
>> #  Eric Lucas
>> #
>> #                "Oh, I have slipped the surly bond of earth
>> #                 And danced the skies on laughter-silvered wings...
>> #                                        -- John Gillespie Magee Jr
>> Version: GnuPG v1.4.11 (GNU/Linux)
>> Comment: Using GnuPG with Mozilla -
>> f5MAnjAcqr2LCANZIplsO7c0+YXH2c7F
>> =7yU1
>> -----END PGP SIGNATURE-----
>> ___________________________________________________________________________
>> Philadelphia Linux Users Group         --
>> Announcements -
>> General Discussion  --
> ___________________________________________________________________________
> Philadelphia Linux Users Group         --
> Announcements -
> General Discussion  --

Philadelphia Linux Users Group         --
Announcements -
General Discussion  --