Why does Git show my local "pull from remote" when I push my commit to the remote repo?

Grizz

So I am having a little trouble understanding Git. I'm using it on Windows, with a remote (GitLab installed) repo. I have a local copy of the master branch, and I am fairly diligent about using git pull frequently to keep my local copy up-to-date.

So it's release time (of course...isn't that when this happens?) and I want to check in the ONE file I've changed. I type a git pull but get an error:

error: Your local changes to the following files would be overwritten by merge:
        <path>/filename.py
Please, commit your changes or stash them before you can merge.
Aborting

I use git diff and sure enough, another developer fixed a typo in the same spot where I'm trying to commit. A quick git status reveals:

Your branch is behind 'origin/master' by 4 commits, and can be fast-forwarded

So, at a loss, I commit my changes to my local repo.

git add <filename>
git commit -m "reworking to ignore logs"

Now, I can git pull successfully.

git pull origin master 
...
Merge made by the 'recursive' strategy
file1
file2...
....
<filename>
5 files changed, 1 insertion, 63 deletions...

Now, I'm able to push my change up to the remote repo

git push

BUT: here's my question. On the remote server, it shows BOTH my commit AND the file changes caused by my local pull after my commit. That is to say, it basically shows that the remote files changed, but in fact changed to what wass ALREADY in the remote repository.

My boss calls in a panic, asking me why I changed all those (other four) files, and I'm not able to answer. We end up checking out the two commits (current and pre-my-changes) and diffing them, to prove that it was ONLY my one file's change is what is different, before we both breathe a sigh of relief.

So my question is: what am I missing? Why should Git think that I want to see that I changed files to match the remote HEAD, in the remote? I have seen this question about git squashing, and realize that there is a git rebase which may help me, too. Can someone help me understand why Git does this? I just feel as though I am not totally embracing the "distributed" paradigm shift.

torek

Apparently both you and your boss are missing the same thing, and your boss tends towards panic. :-)

Here's what you did, in graphical form drawn as commits. Originally you had this in your repo:

... - F - G    <-- HEAD=master, origin/master

where G is the then-latest version. But there's a typo in one file, so you edited that file in your work-tree, fixing the problem. (I'm going to name the file typo just for convenience below.)

$ vim typo
...

You haven't committed this yet, but you did change it. Then you did:

$ git pull

which got you that error message.

Now, behind the scenes, as it were, git pull is just git fetch followed by git merge. So let's look at what git fetch did. It seems that other developers made four commits; let's draw them into the graphical representation of the commit-tree:

              H - I - J - K      <-- origin/master
            /
... - F - G                      <-- HEAD=master

The git merge failed because someone else modified file typo too.

So, now you do:

$ git add typo
$ git commit -m "reworking to ignore logs"

This adds a new commit on your current branch (whatever HEAD points to), which is obviously master. This gives a commit tree that looks like this:

              H - I - J - K      <-- origin/master
            /
... - F - G ------------- L      <-- HEAD=master

Now you run git pull again, which again is just fetch followed by merge. The fetch has nothing to fetch this time (commits H through K, and the label origin/master, are already all taken care of). The merge then merges commit K (origin/master) into your current branch, master, creating a new merge commit M:

              H - I - J - K      <-- origin/master
            /               \
... - F - G ------------- L - M  <-- HEAD=master

If you now git push the result, commit M goes in, merging your change, L, as the first parent of the merge, and their commit K as the second parent.

That's the actual history of what happened to produce M (as hobbs noted in comment) and is perfectly normal. However, you (and your panicky boss) might prefer to construct a different history, that pretends you noticed the problem in typo after you picked up commits H through K. To do that, you'd "rebase" your commit (L, above) to sit on top of origin/master.

You can do this with git pull --rebase (as in hobbs' answer that appeared while I was typing up this long one), or with plain old git rebase after doing a git fetch. In this case, what you will do is make a copy of the changes in commit L (people often spell this "commit L'"):

              H - I - J - K - L'
            /
... - F - G ------------- L

At this point, you don't need commit L any more—it will stick around in the repo for 90 days or so because of git's "reflogs", but it will vanish from normal git log output, for instance—so you just need to re-draw this graph a bit, leaving out the line going to L, and stick the labels on like so:

              H - I - J - K      <-- origin/master
            /               \
... - F - G                   L' <-- HEAD=master

and this is exactly what the git rebase in the command-sequence:

$ git add typo
$ git commit -m "reworking to ignore logs"
$ git rebase

would have done. (Or, again, you can commit and then use git pull --rebase, which just makes git use the fetch-then-rebase sequence, instead of fetch-then-merge. If there's nothing to fetch, as in this case, that's the same as just running git rebase.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can I add a file to my local Git repo but not push it to remote

From Dev

Git: Pull from remote and merge with local work

From Dev

Git: Pull from remote and merge with local work

From Dev

unable to push my local git repo to the remote repo

From Java

Why is `git push --force-with-lease` failing with "rejected ... stale info" even when my local repo is up to date with remote?

From Dev

fatal: Could not read from remote repository. When I push my local repo to remote server

From Dev

How to pull from remote 'A' to remote 'B' in Git

From Dev

How do I tell git to ignore my local changes, but leave the file in my remote repo?

From Dev

Why does git remote prune origin remove my local tags?

From Dev

git pull from remote but no such ref was fetched?

From Dev

Git pull from remote bare repository

From Dev

Why am I getting an empty repo when cloning my keystone app to local repo from heroku?

From Dev

reassign an existing remote repo to my local repo

From Dev

Can I change my .gitignore at a remote site and will it push to GITHUB repro when I do a git push?

From Dev

I want to add remote branch of my local repo?

From Dev

Why can't I simply push 1 file to my repo without having to do a git pull?

From Dev

Why I can't add this remote GitHub repository to my local GIT repository as remote?

From Dev

Cannot push to heroku after adding a remote heroku repo to my existing local repo

From Dev

What does git do, when I push a local branch that does not exist on remote server?

From Dev

Why does my website show lag in Chrome when I scroll?

From Dev

Why does my <img> resize when I use show() and hide()?

From Dev

When a commit was pushed into a remote Git Repo

From Dev

Force git to update my local repo when pulling

From Dev

Why when I checkout a new branch using Git and push this new branch to origin repo, the base commit ID is 0000000000000000000000000000000000000000?

From Dev

How can I build my local git repo on external server?

From Dev

Push local new Git repo to existing remote repo as branch?

From Dev

how to push my code to my VPS once I push to git repo?

From Dev

Automatic pull from remote

From Dev

How to commit my files in a git bare repo

Related Related

  1. 1

    Can I add a file to my local Git repo but not push it to remote

  2. 2

    Git: Pull from remote and merge with local work

  3. 3

    Git: Pull from remote and merge with local work

  4. 4

    unable to push my local git repo to the remote repo

  5. 5

    Why is `git push --force-with-lease` failing with "rejected ... stale info" even when my local repo is up to date with remote?

  6. 6

    fatal: Could not read from remote repository. When I push my local repo to remote server

  7. 7

    How to pull from remote 'A' to remote 'B' in Git

  8. 8

    How do I tell git to ignore my local changes, but leave the file in my remote repo?

  9. 9

    Why does git remote prune origin remove my local tags?

  10. 10

    git pull from remote but no such ref was fetched?

  11. 11

    Git pull from remote bare repository

  12. 12

    Why am I getting an empty repo when cloning my keystone app to local repo from heroku?

  13. 13

    reassign an existing remote repo to my local repo

  14. 14

    Can I change my .gitignore at a remote site and will it push to GITHUB repro when I do a git push?

  15. 15

    I want to add remote branch of my local repo?

  16. 16

    Why can't I simply push 1 file to my repo without having to do a git pull?

  17. 17

    Why I can't add this remote GitHub repository to my local GIT repository as remote?

  18. 18

    Cannot push to heroku after adding a remote heroku repo to my existing local repo

  19. 19

    What does git do, when I push a local branch that does not exist on remote server?

  20. 20

    Why does my website show lag in Chrome when I scroll?

  21. 21

    Why does my <img> resize when I use show() and hide()?

  22. 22

    When a commit was pushed into a remote Git Repo

  23. 23

    Force git to update my local repo when pulling

  24. 24

    Why when I checkout a new branch using Git and push this new branch to origin repo, the base commit ID is 0000000000000000000000000000000000000000?

  25. 25

    How can I build my local git repo on external server?

  26. 26

    Push local new Git repo to existing remote repo as branch?

  27. 27

    how to push my code to my VPS once I push to git repo?

  28. 28

    Automatic pull from remote

  29. 29

    How to commit my files in a git bare repo

HotTag

Archive