0

In git I just did a

git pull

to get any possible changes that someone else might have been made on the remote branch I am on, and that git pull created an automated merge I had to revert with

git reset --hard <commit-just-before>

Is there a way to avoid this automatic merge when doing git pull?

There is the possibility I am doing things completely wrong, as I am a beginner with git and I am not familiar with the nomenclature used in these context.

Alex
  • 41,580
  • 88
  • 260
  • 469
  • 3
    do not use `pull` just use `fetch` (the only difference is it doesn't merge/rebase anything). – Marek R Jul 20 '23 at 11:34
  • Ah ok, but then what is `pull` for? – Alex Jul 20 '23 at 11:36
  • 1
    Do not overthink this. Usually when you are fetch/pull code from remote repository is when you like to update your current working branch, so that it why `pull` implicitly merges. This is convenience shorthand for `fetch` and `merge`. – Marek R Jul 20 '23 at 11:39
  • you use `pull` when you.... _want to get changes from the upstream branch into your local branch_... which will more than likely produce a merge, under normal conditions (there are config values that can ask it to do other things and there are conditions under which git would do a `ff` instead of a merge). – eftshift0 Jul 20 '23 at 11:41
  • I want to get the changes from the upstream branch. But not merging something because I want just to get the changes. – Alex Jul 20 '23 at 11:41
  • Why do you want to get the changes? Do you need them to complete your own task, or are you just curious what they did? Or is your only task to consolidate your work with their work? – j6t Jul 20 '23 at 11:57
  • 1
    Does this answer your question? [Git pull results in extraneous "Merge branch" messages in commit log](https://stackoverflow.com/questions/8509396/git-pull-results-in-extraneous-merge-branch-messages-in-commit-log) – Toby Speight Jul 20 '23 at 12:11
  • I need those changes in order to TEST the code. – Alex Jul 20 '23 at 12:14

2 Answers2

1

You could check whether your remote branch contains new commits:

git fetch
git log ..origin

Add -p to include code changes into the commit log:

git fetch
git log ..origin -p

If you want to see changes without commits:

git fetch
git diff ...origin

But if you want to merge without a commit anyway:

git fetch
git merge --no-ff --no-commit
Automatic merge went well; stopped before committing as requested

To clean the index after this:

git reset --hard
Alexander Nenashev
  • 8,775
  • 2
  • 6
  • 17
1

In current versions¹ of git you can adjust git's config like this:

git config --global pull.ff only

Then git pull will abort instead of attempting a merge, with the following message:

fatal: Not possible to fast-forward, aborting.

If you ever want to override this and pull+merge anyway, you can do that with

git pull --no-ff

See also: https://git-scm.com/docs/git-config#Documentation/git-config.txt-pullff

¹ Version 2.1+ I think, check git --version

Jay
  • 3,640
  • 12
  • 17
  • I tried the `git pull -no-ff`, and although I have NOT MADE ANY CHANGES to any files in the repo I now have merge conflicts. How is that even possible??? – Alex Jul 21 '23 at 12:38
  • 1
    @Alex If you're absolutely sure that you made no changes, then the only explanation I can think of is that somebody changed the commit history upstream by force-pushing. Git then only sees that there are different commits and tries to merge them into a single history. It doesn't know that commits may have been discarded on purpose. Anyway, if that's the case you can reset your local branch to whatever commit upstream is pointing at with `git reset --hard HEAD@{upstream}` (UNCOMMITTED LOCAL CHANGES WILL BE LOST. Locally committed changes can still be recovered if needed, e.g. via `git reflog`) – Jay Jul 21 '23 at 17:19