11

Is there a way to say this more concisely?

git checkout master
git merge branch

What I actually want to do much of the time is:

git rebase master branch
git checkout master
git merge branch

Answer https://stackoverflow.com/a/12343727/313842 sort of touched on this but leaves branch checkout out. git merge in one command is also a similar but different question.

5
  • 6
    git checkout master && git merge branch ;-) Commented Jan 6, 2016 at 11:19
  • 1
    Maybe you can create a script or symlink which bundles together some Git commands. Commented Jan 6, 2016 at 11:25
  • Sure I can write a script, or whatever. I just wanted to see if there was a way, which I had missed, to do it with a single command. Commented Jan 7, 2016 at 14:34
  • 1
    In the scenario you describe you can do git checkout -B master. It performs a reset instead of a (fast-forward) merge, but that's safe since you did a rebase master first. This solution is also mentioned in an alternative answer to the one you linked to. Commented May 30, 2016 at 19:28
  • That is a neat idea - saves one step. Thanks. Commented Jun 1, 2016 at 6:42

2 Answers 2

25

If your branch names are generally long, and you constantly merge the branch you just checkout from, you can use:

checking out from branch you want to merge

git checkout branch
git merge -

the '-' is shorthand for the previous branch, very handy for quick checking out and merging

Sign up to request clarification or add additional context in comments.

Comments

4

You have several options:

  • Script
    Write a script which execute your commands
  • git alias
    Write it in an alias or function inside your .gitconfig file
  • Use & operator

    git checkout master && git merge branch & ... & ...
    

2 Comments

"Use & operator" I think you mean "Use && operator"
The & operator is the Windows cmd.exe equivalent of ; in bash: you can run multiple commands in sequence regardless of whether the previous one failed. Use && between commands to only run if the previous command returned a 0 errorcode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.