views:

120

answers:

3

Hi.
I would like to merge from FeatureBranch to master, without doing ,,checkout master'' first. I've tried (being in FeatureBranch)

git push . master

but I got (to a degree of surprise) :

Everything up-to-date

Despite having commits in FeatureBranch which are not (yet) present in master.

The reasons why I desire to be able to do ,,one-step local push'', are:

  1. I want to give changes to my co-workers who stay on master branch
  2. without the additional step of ,,checkout master''
  3. thus being able to still stay in FeatureBranch
  4. and avoiding rapid changing of many files which confuses/alerts many tools which have something to do with dirs/files in repo

I know I can do it in more steps in different ways. But I wonder whether there is a one-step solution to this (I think there should be).

I think/realize, that if conflicts arise, I would have to switch to master anyway. But in most cases I do not have conflicts and thus would benefit from one-step solution.

My git version:

git --version
git version 1.6.5.1.1367.gcd48

(Windows)

TIA
karolrvn

+1  A: 

You cannot commit to a branch other than the current one; or to put it another way, the new node created by git commit is always a child of the current HEAD.

I have occasionally used a separate 'staging' repo in order to avoid the overhead of switching branches in my live development environment:

# Currently working on branch 'foo' in $SOME_DIR/main-repo
# main-repo is a local clone of shared-repo

# Create the staging repo alongside the existing main-repo
cd $SOME_DIR
git clone shared-repo staging-repo
cd staging-repo
git remote add local ../main-repo

# Switch back to main-repo and continue working
cd main-repo
# (Make changes and commit to branch foo ...)

# Switch to the staging repo
cd $SOME_DIR/staging-repo

# Make sure we are up to date with shared repo (*)
git pull

# Merge changes from main-repo
git fetch local
git merge local/foo

# Push changes up to the shared repo
git push

A potential problem with this approach is that it doesn't allow you to test the result of merging in the changes made on branch 'foo' with any that have meanwhile been made in shared-repo/master (*). Depending on the nature of the changes, this may be OK, but in most cases you will want to at least do a quick sanity check (e.g. check that the code still compiles, maybe run smoke tests) before pushing to the shared repository.

In order to do that, you would either need to:

  1. build staging-repo - but in this case, the merge could just have been done directly in main-repo
  2. have staging-repo in a separate build environment from main-repo, i.e. $SOME_OTHER_DIR/staging-repo. This would allow staging-repo to be built and/or tested without dirtying the environment of main-repo.
Gareth Stockwell
Thanks for response. Note that I'm not talking about making new commits: only about adding existing commits to a branch. Or maybe you generalized the idea?
karolrvn
Note my question (in comment) about what ,,git push . master'' does, above.
karolrvn
Wouldn't it be nice/probable, that such an operation, as I ask about, would one day be implemented in git? Is it ,,not _yet_ implemented'' or ,,not possible - by design'' or ,,possible, but not ,,the git way'' '' or maybe noone cares enough; or some other scenario?
karolrvn
BTW the clone process takes a few minutes on our repo.
karolrvn
@karolrvn Regarding your first question: adding existing commits (in my example, from main-repo/foo) to a branch (staging-repo/foo) is done by the "git merge local/foo" step.
Gareth Stockwell
@karolrvn Regarding whether git may be modified so that "git push" can be used to propagate commits between branches in the same repository: I would say the answer is no, for the reason explained in calmh's answer.
Gareth Stockwell
A: 

It seems that what you actually want to do is a merge, not a push? Unfortunately I don't think that's possible without being on the branch that is the target of the merge, so the command sequence would be:

git checkout master
git merge FeatureBranch
git checkout FeatureBranch

This entire operation should take less than a second, so I'm not really sure why you are strongly objecting to changing to the master branch for this...

calmh
Thx for response. I'm not ,,strongly objecting'' I just want to omit a step that is not necessary from user viewpoint (but may be necessary from git's viewpoint for ,,technical reasons''). Have you ever had situations after doing checkout, that lots of tools (e.g. text editors) start asking you about missing files, opened files which were changed/deleted ,,by some other process''?Besides: (from a ,,subjective'' point of view of a user) why should I want/need to go to a state which is no longer needed (the ,,left behind'' state of master branch) and with which I don't want to do anything more.
karolrvn
An exception would, perhaps, be when a merge conflict happens. But in my case this is unlikely most of the time (we rarely have conflicts because we usually work on separate parts).
karolrvn
I get what you're saying, and the point about editors not liking when the files change under them is very valid. The "technical reasons" in git's case might be that it wants you to be on the branch that is going to be modified, `master` in this case. This so that you don't modify things whose state you can't see.
calmh
But I'm just psychoanalyzing git here, I don't know the real reasons. :)
calmh
You could of course also do this from a separate clone of the repository, to avoid messing with the one you are doing development in. In that repository, stay on the `master` branch, and repeatedly pull form your development repository's `FeatureBranch`, which will cause a merge that in your case will probably be a fast forward. Then push, or whatever.
calmh
+1  A: 

Regarding your question about why the push command acts like it does...

Push is used to propagate commits from one repository to another, not among branches within a repository. In your case you are pushing from one repository to the same repository, so the answer is "Everything up to date", just as it should be. The fact that you are naming two different branches is irrelevant in this case.

calmh