4

I constantly need to update my remote branch with current master. I am trying to automate this as;

#!/usr/bin/env bash

cd directory
git checkout master 
git pull
git checkout <remote_branch>
git pull origin <remote_branch>
git merge master
git push

but when I merge master I am thrown into my editor where I need to :wq to be able to push. How can I replicate this :wq in my Bash script?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Huseyin Cakar
  • 51
  • 2
  • 3

1 Answers1

6

There are 3 ways listed below to do this and you don’t have to replicate :wqto do so.

1 - Using --no-edit

using the --no-edit option, the auto-generated message will be accepted (this is generally discouraged, see the git docs)

So after you git pull origin <remote_branch>, you can use

git merge master --no-edit

2 - Using environement variable

Older scripts may depend on the historical behaviour of not allowing the user to edit the merge log message. They will see an editor opened when they run git merge. To make it easier to adjust such scripts to the updated behaviour, the environment variable GIT_MERGE_AUTOEDIT can be set to no at the beginning of them. — git docs

Use

GIT_MERGE_AUTOEDIT=no

before the merge line

3 - Use -m to provide the message

Set the commit message to be used for the merge commit (in case one is created).

If --log is specified, a shortlog of the commits being merged will be appended to the specified message.

The git fmt-merge-msg command can be used to give a good default for automated git merge invocations. The automated message can include the branch description. — git docs

git merge master -m "Merge master"

Read more: the git merge command

toydarian
  • 4,246
  • 5
  • 23
  • 35
hedy
  • 1,160
  • 5
  • 23