1

My current workflow is:

  1. work on some branch work_branch
  2. commit the changes
  3. Want to merge into master, but still work on that same branch later
  4. checkout master
  5. merge work_branch into master
  6. checkout work_branch

I would like to save myself the hassle of checking out master and then checking out work_branch.
Instead, I would like to somehow "push" the changes to master, without making it the active branch.

I was not able to find a duplicate on this, but I guess I just lack the terminology.

underscore_d
  • 6,309
  • 3
  • 38
  • 64
Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • It looks like https://stackoverflow.com/questions/13897717/push-commits-to-another-branch – Moshe Fortgang Jul 24 '19 at 15:45
  • 1
    If you need to do actual merging (i.e., the `git merge` that you will do in step 5 results in a true merge rather than a simple fast-forward), you *need* another index-and-work-tree in which to do the merge. In that case, see [Calum Halpin's answer](https://stackoverflow.com/a/57187127/1256452) (or make another repository and push/fetch between these two repositories and do the work in the other repository). But if your update to `master` is always a fast-forward, there *is* a cheat-y method, using either `git fetch` or `git push` from your own repository *to* your own repository. – torek Jul 24 '19 at 16:12
  • 1
    Essentially, you can run `git push . work_branch:master`, which says: *Hey Git, call up another Git (which is really yourself). Then ask that other Git to set its `master` the same as my `work_branch`.* This means your Git asks itself to set its own `master` the same as its own `work_branch` ... which is the non-checkout half of what a fast-forward merge *is*, so if the fast-forward operation succeeds, you're now done. – torek Jul 24 '19 at 16:14

1 Answers1

3

No, there's no way to do this. From the git-merge man page:

Incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch.

Git is structured such that commands like that operate on the currently checked out commit.

The closest thing I can think of would be setting up a second working tree with git worktree, which would allow you to checkout your master branch and merge from your working branch whilst still having your working branch checked out.

Calum Halpin
  • 1,945
  • 1
  • 10
  • 20