Search code examples
gitgithubgit-pull

How to correctly merge pull request and reconcile local repository


When I merge a pull request on GitHub and then run git pull -a on the local repository I found another commit: Merge branches 'blabla' and 'main' of github.com:repository, and in the log, there is written: Merge made by the 'ort' strategy. Then I'm forced to push this commit.

Steps to reproduce:

  1. Create a repository and commit
  2. Create a branch and make some changes, then push
  3. On the GitHub portal click "Create a pull request" and merge
  4. On your local repository run git pull -a on the main branch
  5. The new commit appears.
  6. now you must run git push

The problem is that on the GitHub repository, there are Actions workflows that run when a commit is done on the main branch.

What is the correct workflow to do a pull request and reconcile the local repo without creating another commit?

Edit: I'm the only person who interacts with the repo


Solution

  • There's a couple of details about git that lead to this behavior:

    1. Your local main branch and origin/main are completely independent branches.

    2. git fetch origin downloads any changes from the origin repo (e.g. new commits or new branches), but it does not merge them with your local branch (i.e. your local main branch remains untouched). To actually see the changes on your file system, you need to check them out, e.g. using git reset --hard origin/main.

    3. git pull origin main performs a git fetch followed by a git merge. Usually, git will perform a so-called fast-forward merge where it will simply advance your local main to origin/main, but if that's not possible for some reason, it will create a full merge commit instead.

    For this reason, I am always using the following workflow and I'd recommend you do the same:

    • Never use git pull as it might have unintended side effects.
    • Instead, always use git fetch origin followed by git reset --hard origin/main (replace the branch name with whatever you are currently working on).

    Alternatively, you can use a UI like GitKraken, Git Extensions or SmartGit which will visualize the current situation for you.

    If you want to dig deeper, I highly suggest this YouTube video that explained it to me.