J.Doe
J.Doe

Reputation: 31

How can I reset another branch to the origin without checking it out?

A typical workflow I have is to checkout master and type git reset --hard @{u}. From there I will checkout the branch I want to merge master with and merge master.

Can I perform the steps of updating master without changing to it? (This would help me not stash when changing)

For example, I am on branch A which was branched from master. master has been worked on and I have a lot of changes and don't want to perform a stash, so instead of performing a

  1. git stash
  2. git checkout master
  3. git reset --hard @{u}
  4. git checkout A
  5. git merge master
  6. git pop stash (pseudo)

I want to be able to perform a git reset --hard @{u} for master while currently on branch A,

Upvotes: 2

Views: 486

Answers (2)

ElpieKay
ElpieKay

Reputation: 30858

Try git worktree.

git worktree add /path/to/foo master
cd /path/to/foo
git reset --hard @{u}
# remove the working tree when it's not needed any more
git worktree remove /path/to/foo

It checks out master in another working tree and does not affect the current working tree where A has been checked out.

But, why would you insist on using git reset --hard? git update-ref is indeed a better solution.

Upvotes: 0

Acorn
Acorn

Reputation: 26066

Can I perform the steps of updating master without changing to it?

Yes, see this answer.

I don't think those answers involve performing a hard reset on it.

A hard reset does not make sense when you are not checking out the other branch.

If you weren't doing a merge, you could also avoid the stash by using a soft reset rather than a mixed one, too.

So (almost) all your steps condense to a single branch or soft reset command as explained in that answer.


In general, your workflow seems strange. To update master to be in sync with upstream, you would normally fast-forward merge the changes. But there is no need to do so, you can base your work branch on top of a remote branch after a fetch.

Furthermore, you would normally either merge master into your local branch to keep them in sync, or rebase your branch on top of master; depending on your style of workflow (whether you want history to be more linear or not).

Using stash, in general, should be a rare thing to do.

Upvotes: 1

Related Questions