diff --git a/marge/git.py b/marge/git.py
index 29a7684..df8cfc5 100644
--- a/marge/git.py
+++ b/marge/git.py
@@ -107,9 +107,9 @@ class Repo(namedtuple('Repo', 'remote_url local_path ssh_key_file timeout')):
             raise
         return self.get_commit_hash()
 
-    def remove_branch(self, branch):
-        assert branch != 'master'
-        self.git('checkout', 'master', '--')
+    def remove_branch(self, branch, *, new_current_branch='master'):
+        assert branch != new_current_branch
+        self.git('checkout', new_current_branch, '--')
         self.git('branch', '-D', branch)
 
     def push_force(self, branch, source_repo_url=None):
diff --git a/marge/job.py b/marge/job.py
index 613a649..c7196c9 100644
--- a/marge/job.py
+++ b/marge/job.py
@@ -363,8 +363,8 @@ def update_from_target_branch_and_push(
         # A failure to clean up probably means something is fucked with the git repo
         # and likely explains any previous failure, so it will better to just
         # raise a GitError
-        if source_branch != 'master':
-            repo.remove_branch(source_branch)
+        if source_branch != target_branch:
+            repo.remove_branch(source_branch, new_current_branch=target_branch)
         else:
             assert source_repo_url is not None
 

Don't assume a branch called `master` exists when removing the merged branch
It does not always merge request should be merged into `master`. E.g., in some git workflows, MR should be merged into `develop` branch. I propose to add configuration parameter `--branch-name-merge-into <string name>` for such situations.
diff --git a/tests/test_git.py b/tests/test_git.py
index 9ceaa1a..2ebc79c 100644
--- a/tests/test_git.py
+++ b/tests/test_git.py
@@ -112,6 +112,13 @@ class TestRepo(object):
         assert get_calls(mocked_run) == []
 
     def test_remove_branch(self, mocked_run):
+        self.repo.remove_branch('some_branch', new_current_branch='devel')
+        assert get_calls(mocked_run) == [
+            'git -C /tmp/local/path checkout devel --',
+            'git -C /tmp/local/path branch -D some_branch',
+        ]
+
+    def test_remove_branch_default(self, mocked_run):
         self.repo.remove_branch('some_branch')
         assert get_calls(mocked_run) == [
             'git -C /tmp/local/path checkout master --',
@@ -120,7 +127,7 @@ class TestRepo(object):
 
     def test_remove_master_branch_fails(self, unused_mocked_run):
         with pytest.raises(AssertionError):
-            self.repo.remove_branch('master')
+            self.repo.remove_branch('meister', new_current_branch='meister')
 
     def test_push_force(self, mocked_run):
         mocked_run.return_value = mocked_stdout(b'')

pip install -e .[dev]
parse_log_pytest
requirements.txt
apt-get update apt-get install -y gcc
3.9
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
astroid==3.3.9
certifi==2025.1.31
charset-normalizer==3.4.1
ConfigArgParse==1.7
coverage==7.8.0
dateparser==1.2.1
dill==0.3.9
exceptiongroup==1.2.2
flake8==7.2.0
humanize==4.12.2
idna==3.10
iniconfig==2.1.0
isort==6.0.1
-e git+https://github.com/smarkets/marge-bot.git@9986daf294673ad58a06c7ca19125bc20c144c96#egg=marge
maya==0.6.1
mccabe==0.7.0
packaging==24.2
pendulum==3.0.0
platformdirs==4.3.7
pluggy==1.5.0
pycodestyle==2.13.0
pyflakes==3.3.1
pylint==3.3.6
pytest==8.3.5
pytest-cov==6.0.0
pytest-flake8==1.3.0
pytest-pylint==0.21.0
pytest-runner==6.0.1
python-dateutil==2.9.0.post0
pytz==2025.2
PyYAML==6.0.2
regex==2024.11.6
requests==2.32.3
six==1.17.0
snaptime==0.2.4
time-machine==2.16.0
tomli==2.2.1
tomlkit==0.13.2
typing_extensions==4.13.0
tzdata==2025.2
tzlocal==5.3.1
urllib3==2.3.0

name: marge-bot
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - astroid==3.3.9
      - certifi==2025.1.31
      - charset-normalizer==3.4.1
      - configargparse==1.7
      - coverage==7.8.0
      - dateparser==1.2.1
      - dill==0.3.9
      - exceptiongroup==1.2.2
      - flake8==7.2.0
      - humanize==4.12.2
      - idna==3.10
      - iniconfig==2.1.0
      - isort==6.0.1
      - maya==0.6.1
      - mccabe==0.7.0
      - packaging==24.2
      - pendulum==3.0.0
      - platformdirs==4.3.7
      - pluggy==1.5.0
      - pycodestyle==2.13.0
      - pyflakes==3.3.1
      - pylint==3.3.6
      - pytest==8.3.5
      - pytest-cov==6.0.0
      - pytest-flake8==1.3.0
      - pytest-pylint==0.21.0
      - pytest-runner==6.0.1
      - python-dateutil==2.9.0.post0
      - pytz==2025.2
      - pyyaml==6.0.2
      - regex==2024.11.6
      - requests==2.32.3
      - six==1.17.0
      - snaptime==0.2.4
      - time-machine==2.16.0
      - tomli==2.2.1
      - tomlkit==0.13.2
      - typing-extensions==4.13.0
      - tzdata==2025.2
      - tzlocal==5.3.1
      - urllib3==2.3.0
prefix: /opt/conda/envs/marge-bot

tests/test_git.py::TestRepo::test_remove_branch tests/test_git.py::TestRepo::test_remove_master_branch_fails
tests/test_git.py::PYLINT
tests/test_git.py::flake-8::FLAKE8 tests/test_git.py::TestRepo::test_clone tests/test_git.py::TestRepo::test_config_user_info tests/test_git.py::TestRepo::test_rebase_success tests/test_git.py::TestRepo::test_merge_success tests/test_git.py::TestRepo::test_reviewer_tagging_success tests/test_git.py::TestRepo::test_reviewer_tagging_failure tests/test_git.py::TestRepo::test_rebase_same_branch tests/test_git.py::TestRepo::test_merge_same_branch tests/test_git.py::TestRepo::test_remove_branch_default tests/test_git.py::TestRepo::test_push_force tests/test_git.py::TestRepo::test_push_force_fails_on_dirty tests/test_git.py::TestRepo::test_push_force_fails_on_untracked tests/test_git.py::TestRepo::test_get_commit_hash tests/test_git.py::TestRepo::test_passes_ssh_key tests/test_git.py::test_filter tests/test_git.py::test_filter_fails_on_empty_commit_messages tests/test_git.py::test_filter_fails_on_commit_messages_that_are_empty_apart_from_trailers tests/test_git.py::test_filter_ignore_first_line_trailer_in_commit_message_if_not_set
marge/git.py marge/job.py
marge/git.py marge/job.py