title: How to checkout and merge branches to master - GITPYTHON
body: <p>I need to merge all my branches to the master using python. branch local path,buildno getting via environment variables. I have the following code to checkout each branch and merge to master. but it gives some errors when executing the code.</p>
  
  <pre><code>from git import Repo
  import git
  
  def mergeRepo(user, buildno, repo, branches, gdwlocalpath, ref="master" ):
  
  branch_list = branches.split(",")
  repo = git.Repo(gdwlocalpath)
  repo.git.checkout(ref)
  current = repo.active_branch
  remote_branches = []
  for ref in repo.git.branch('-r').split('\n'):
      remote_branches.append(ref.replace("origin/","").strip())
  print(remote_branches)
  
  mergeFailure = False
  mergeResult = None
  prefix='origin/'
  
  for branch in branch_list:
      if branch in remote_branches:
          print(prefix+branch)
          current=repo.git.checkout(prefix+branch)
          master = repo.git.checkout('master')
          base = repo.merge_base( master,current)
          repo.git.index.merge_tree(master, base=base)
          repo.index.commit("Merge into master",parent_commits=(current.commit, master.commit))
          current.checkout(force=True)
      else:
          print("Branch doesn't exist:",branch)
  </code></pre>
  
  <p>when I execute the code, it gives the following error. (I manually modified the om.docker.dev/xxx/releasekit/VERSION and commit to bug branch and pushed to the bug branch' and trying to execute the script, expected behaviour is new VERSION file should be merge with the master. </p>
  
  <p>error is actually  correct,as my bug branch is ahead of 1 commit to master, but I need to merge that commit to master. but it gives 128 exit code.</p>
  
  <p>if conflict happens it should display an error. (I dont have any exceptions at the moment). if I want to add any exceptions for merge failures and merge conflict how should I modify this. second question is why it is giving exit code 128. </p>
  
  <p>im using python 3.6</p>
  
  <pre><code> Traceback (most recent call last):
    File "./SingleMergeUp.py", line 82, in &lt;module&gt;
      mergeRepo(username,buildno,arguments.repo,arguments.sources,gdwpath)
    File "./SingleMergeUp.py", line 40, in mergeRepo
      base = repo.merge_base( master,current)
    File "/Users/ab/.virtualenvs/python_core/lib/python3.6/site-packages/git/repo/base.py", line 490, in merge_base
      lines = self.git.merge_base(*rev, **kwargs).splitlines()
    File "/Users/ab/.virtualenvs/python_core/lib/python3.6/site-packages/git/cmd.py", line 440, in &lt;lambda&gt;
      return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
    File "/Users/ab/.virtualenvs/python_core/lib/python3.6/site-packages/git/cmd.py", line 834, in _call_process
      return self.execute(make_call(), **_kwargs)
    File "/Users/ab/.virtualenvs/python_core/lib/python3.6/site-packages/git/cmd.py", line 627, in execute
      raise GitCommandError(command, status, stderr_value)
  git.exc.GitCommandError: 'git merge-base Your branch is ahead of 'origin/master' by 1 commit.
    (use "git push" to publish your local commits) ' returned with exit code 128
  stderr: 'fatal: Not a valid object name Your branch is ahead of 'origin/master' by 1 commit.
    (use "git push" to publish your local commits)'
  </code></pre>