Search code examples
gitgit-mergegit-fetch

git fetch - reviewing and modifying incoming changes


I have read that git pull = git fetch + git merge, and that the latter is usually preferred because it allows one to review changes before merging them.

Our small development team is sharing a git repo on a server. My colleague just pushed and I fetched, so that his commits are now in my local repository. I can see them with:

git log ..origin/mybranch

and inspect them with:

git diff <hash>

Now let's imagine that I want to merge the changes into my working copy, but I don't like some commits or parts of one commit.
My question is:

  • how do I go about "modifying" a commit before merging it into my working copy?
  • in case I can do the above, will that affect the remote repository? (I have read that one should not rebase after a push, for example)
  • in case I cannot do the above, how do I fix the changes after merging them? (eg. manually, ...)

In short: can someone give an overview of actions that are typically performed between a "fetch" and a "merge" to review and edit changes?


Solution

  • You could do a merge without committing the result:

    git merge --no-ff --no-commit <hash>
    

    Then you reset the index (but not the working tree which reflects the merge)

    git reset 
    

    Finally you can git add any file or even git add --patch part of files you want to keep in the result of that merge.
    Or you can reject a merged file entirely: git checkout HEAD -- aFile.

    You have other options in "How do you merge selective files with git merge?"