2

My local repo is ahead of the remote. In the SourceTree view, my local repo (top line) is ahead of remote, or "origin/master" (second line) by 9 commits.

This is confirmed with git status: Your branch is ahead of 'origin/master' by 9 commits. (use "git push" to publish your local commits) nothing to commit, working directory clean

But, I don't want to just do a git push because I want to put my 9 commits on a branch or feature.

Specifically, I want to: - reset my local install back to origin/master - add a new feature or branch that splits off from the master - commit my changes - push the feature branch

I'm confused about the first step: Resetting my local back to the origin/master. I believe a rebase would do this from command line but how would I do this within SourceTree?

I've searched this topic and answers advise a 'git push', but I want to do the push from the new feature branch, all in SourceTree.

Thanks.

2
  • So, the question is: do you want to discard or keep the changes?
    – Makoto
    Commented Oct 1, 2015 at 1:05
  • I'd like to keep my changes (the 9 commits), if possible, and put them on a new branch, after resetting my local install back to origin/master. Commented Oct 1, 2015 at 1:13

2 Answers 2

5

First if you want to keep your current changes and set them to a remote branch do this:

git commit -a -m "Save all my work on this branch"
git branch saveThisFeature

(you can name the branch anything btw)

Then if you want to reset the master branch to the last push you need to do this:

git fetch origin
git reset --hard origin/master

Then you can view your master branch that you are on or switch over to the saveThis branch by:

git checkout saveThisFeature

Here is a quick explanation of the -a and -m git options:

-m Commit changes to head (but not yet to the remote repository)

-a Commit any files you've added with git add, and also commit any files you've changed since then

2

Since you haven't pushed anything, this will make it somewhat easy. Note that I'm not using any UI tools since I'm more familiar with the command line.

git reset HEAD~9
git checkout -b <branch-name>
git add .
git commit

The reset introduces a mixed mode reset; you won't lose any of your work, but it will move the head pointer of your master branch back 9 commits.

If the work you have is where it should be; that is, the work that should have been on its own branch now is, then you should look to force-push your master branch, thus overwriting history. Be careful: if anyone else has built their work off of the tip of master, they will have to rebase their work against your new branch instead.

2
  • I like the simplicity of this solution. I did the git reset~9 which successfully put me on the master. But also told me I am now 5 behind and can fast-forward and 'git pull' to update my local branch. So, I did a git pull which gave me errors about local changes that would be overwritten by merge. (I probably should have done the git checkout -b before the pull). Can I do the git checkout -b <branch-name> at this point? If it's less complicated to just overwrite my local changes, that would be OK - I have backups. Commented Oct 1, 2015 at 19:45
  • Right, I suppose that's the next part of it. (I wrote the above while I was on a plane there...) Anyway, I believed that you wanted to rewrite your history as if the nine commits were never on master, so you would want to actually force-push your master branch to your remote repo. Before you do that, be sure that everyone's cool with it, though, as rewriting history usually messes with someone's day.
    – Makoto
    Commented Oct 1, 2015 at 19:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.