Using git reset to merge

My lead says to merge an issue into the main branch, first I should

git fetch
git checkout main-branch
git reset --hard origin/main-branch # ???
git checkout my-branch
git merge main-branch

This appears to correctly fast-forward merge my local main-branch to match origin. Why is this? Shouldn’t it be

  • Using Git or Mercurial, how would you know when you do a clone or a pull, no one is checking in files (pushing it)?
  • What is the difference between Stop Tracking and Discard File in git SourceTree
  • Remove files added in lots of commits in the past
  • Showing a fancy tag list on git
  • Unstage a deleted file in git
  • Composer install/update not working
  • git merge origin/main-branch
    

    or

    git pull origin main-branch
    

    What is the difference?

  • How to organize a Python project with pickle files?
  • Rails Tutorial localhost can't click “Account” button
  • Unable to see the newly created branch after cloning my own git repository on my laptop
  • How do git remotes work?
  • Resetting remote to a certain commit
  • Specific git branches for aws elastic beanstalk environments
  • 2 Solutions collect form web for “Using git reset to merge”

    git fetch will update your remote branches to have all the new commits in them. So origin/main-branch will be updated afterwards. So when you switch to your local main-branch and reset to the remote branch, you are resetting the local branch to the updated branch from the remote now.

    Usually, you would use git pull on the main-branch to get this effect. This also has the huge benefit that in case you had some local commits that were not included on the remote branch, they would get merged automatically. So, be careful as using reset --hard can result in commits getting lost.

    Btw. git pull is essentially the same as git fetch followed by a git merge, so either is fine. Running git fetch manually helps if you have multiple remote branches you would want to update (as git fetch fetches all remote branches by default).

    Another very safe option would be to use git pull --ff-only. This is what I usually do when updating a local branch I do not expect to have any local commits. In case you ever happened to have a local commit that is not part of the remote branch, it will give you a warning and will not merge anything. So you can figure out first, where the commit came from and see for example if you accidentally commited to a wrong branch or something.

    The combination

    git fetch
    git checkout main-branch
    git merge origin/main-branch
    

    Would be essentially the same as

    git checkout main-branch
    git pull
    

    So that does seem like a lot better than git reset --hard origin/main-branch, which just throws away any local commits.

    The following

    git checkout my-branch
    git merge main-branch
    

    merges changes from main-branch to my-branch.

    To “merge an issue into the main branch” you should rather do (assuming the issue is fixed on my-branch)

    git checkout main-branch
    git merge my-branch
    
    Git Baby is a git and github fan, let's start git clone.