4

When I'm on my branch I want to make pull from the master to get the latest version of code, then I rebase my branch onto master and after I push my branch to the server and create a pull request.

Today I do all of this the next way:

  • I'm on my_branch, I switch to master branch
  • After switch, being on master, I either execute git pull or in Intellij use the graphic interface VCS->git->pull
  • I go back to my_branch and perform the rebase
  • After rebase I make a git push -f

How could I avoid switching from my_branch to master and back in Intellij? Is there a way to pull the master to master being on another branch?

Vitalii
  • 10,091
  • 18
  • 83
  • 151
  • 2
    your local master is always like remote master? If that is the case, you can set up your feature branch to track remote master instead of local and then you could fetch/rebase (or pull --rebase) without having to switch to local master to do pull. – eftshift0 Oct 11 '18 at 12:49
  • yes, `master` is a common branch for everybody and we create new branches and merge back to it. Nobody writes on master (except hot fixes that are quite rare) – Vitalii Oct 11 '18 at 12:59
  • 2
    well....no need to keep a local master... actually, you can keep it... but your feature branches (or whatever) can be started not from local master but from remote master. Then you won't need to switch to local master in order to rebase your feature on top of it, as I explained on my previous comment. – eftshift0 Oct 11 '18 at 13:01
  • https://stackoverflow.com/questions/520650/make-an-existing-git-branch-track-a-remote-branch – eftshift0 Oct 11 '18 at 13:22

2 Answers2

2

Assuming the master branch you talked about is on a remote called upstream, you can run the next 2 commands before pushing your code:

git fetch git rebase upstream/master

If you didn't set any remotes, the default remote name is origin and in that case the commands are:

git fetch git rebase origin/master

git fetch won't change any of your local branches, it will just give git information on what exists on the server.

Here is a related StackOverflow question that you might want to also look at when you have time: How to rebase local branch with remote master

mgershen
  • 1,567
  • 16
  • 22
1

I created git alias exactly for that. Put it into your ~/.gitconfig

[alias]
  update = "!fn() { \
    repo=${2:-origin}; \
    if [[ $(git rev-parse --abbrev-ref HEAD) = \"$1\" ]]; \
    then \
        git pull \"${repo}\"; \
    else \
        git fetch \"${repo}\" \"$1\":\"$1\"; \
    fi; \
    }; fn"

then you can use git update master to update your local master without switching to it. Or, if your remote is not origin but, say, upstream, you can use git update master upstream. What about Intellij, seems that there is no such feature out of the box.

fantom
  • 486
  • 1
  • 6
  • 15