Search code examples
gitgit-rebaseoverwritegit-pull

Are files overwritten after pull request if they were initially included in local commits?


I want to clarify one aspect regarding local commits and subsequent git pull request.

Let's say I have file file1.txt in my repo. I then make some changes to it and run 'git commit' on this file. Then, I 'git pull' from server where this file (file1.txt) has also been changed by some developer. How git is going to track my changes on file1.txt inside my local commit and changes inside the same file from remote server?

Is it going to keep file unchanged inside already created local commit?

git add file1.txt
git commit -m "Included file1.txt inside local commit"
git pull

Solution

  • First of all - a Pull Request and git pull are different

    • Pull Request - is the one you request to admin user to merge your changes to some other branch. You can see that in stash / github (web).
    • git pull - Is the one you are asking for.

    Regarding your question (git pull)
    Almost Yes, there is a chance that it might overwrite your file. It happens when you didn't commit the file changes and there is a conflict or the revision is different. git pull might apply new changes in force.

    I suggest -

    • Do not do git pull
    • Do git fetch and then git rebase

    As this is the best and clean way (without override / without applying changes in force). git rebase will ask you for resolving conflicts if any.

    Also I have seen that git pull = git fetch + git merge . That might be true. But rebase is the safe way because git merge creates new commit (which basically I don't like!!)

    Note:
    There might be developers using or people who likes to use git pull rather than my idea. But my suggestion is as per my experience.