1

I am trying to set up a workflow that with auto-merge to master.

For that, I am working locally on a branch called dev-oshai, and want to push always to that branch on the server with: git push

When I want to pull changes I want to always pull them from master (CI will auto-merge changes to master from all developers).

For that I am using the command git pull --rebase origin master

More context and details here: https://medium.com/@OhadShai/git-mono-branch-workflow-pre-tested-commits-4b34949310ad

Is it possible to configure git that the default pull branch will be origin master so that git pull will work without always specifying the branch?

oshai
  • 14,865
  • 26
  • 84
  • 140
  • i dont see a way to have the mix of pushing to the default and pulling from another default. you can set the upstream like usual but that would be for pulling and pushing... – L_Church Mar 26 '18 at 08:28
  • https://stackoverflow.com/questions/2916845/different-default-remote-tracking-branch-for-git-pull-and-git-push see if that helps – L_Church Mar 26 '18 at 08:29
  • Whenever you call git pull it will always pick up your current branch. I don't think there is a way to define default branch on pull either you can make an alias of this command in your .bashrc file so that you don't have to write the command again and again... – Sakshi Garg Mar 26 '18 at 08:30
  • @L_Church this will set the different repositories for the same code. But the above question is to set different branches for pull and push. – Sakshi Garg Mar 26 '18 at 08:32
  • ah well there goes my ideas i already +1 so this can get out – L_Church Mar 26 '18 at 08:33
  • You can use ElpieKay's answer, but I recommend getting out of the habit of running `git pull` at all, ever. Just run `git fetch` followed by `git rebase`. The whole question at this point kind of goes away: you will use `git rebase origin/master` and `git push origin HEAD:dev-oshai` or similar (you can leave the upstream completely unset). – torek Mar 26 '18 at 19:55

2 Answers2

4
[remote "origin"]
        url = <repo_url>
        fetch = +refs/heads/*:refs/remotes/origin/*
[remote "foo"]
        url = <repo_url>
        push = refs/heads/master:refs/heads/dev-oshai
[branch "master"]
        remote = origin
        pushRemote = foo
        merge = refs/heads/master
        rebase = true

This way, when in branch master, git pull is equivalent to git pull origin -r master:master and git push is equivalent to git push foo master:dev-oshai. The urls of origin and foo are the same.

Besides true, branch.master.rebase can be assigned preserve or interactive.

When preserve, also pass --preserve-merges along to git rebase so that locally committed merge commits will not be flattened by running git pull.

When the value is interactive, the rebase is run in interactive mode.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53
1

I don't think it is possible to change the behaviour for git pull completely. (except creating an alias)
But you can make it a bit less to type.

First, set up pull to always rebase in your gitconfig.

[pull]
    rebase = true

Next, you can do a little hack. Add your remote as a new local remote, so you have it twice.
For example master.

git remote add master https://example.com/repo.git

Fetch all branches

git fetch master

And set the upstream of dev-oshai to master of the new remote.

git branch --set-upstream-to=master/master

Now you can pull from master with

git pull master

If this really is a good idea or more confusing than helpful is your decision.

lukas-reineke
  • 3,132
  • 2
  • 18
  • 26