git - A git fetch / git merge merges without confl

2019-08-21 10:36发布

问题:

I am very new to git and I have been seeing this every once in a while.

For example:

  1. I work on my branch branch-A for few days (branch is created from develop fresh copy)
  2. I do git add . / git commit -m "blahblah" to stage and commit my changes
  3. Now I want to get latest changes from remote and merge it into my branch so I ensure I work on latest code
  4. to do so, I do git checkout develop to switch to my local develop branch, git status shows I'm behind 37 commits

    myMBPro:MyProj$ git status
    On branch develop
    Your branch is behind 'origin/develop' by 37 commits, and can be fast-forwarded.
      (use "git pull" to update your local branch)
    
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    

    and the list of deleted, untracked files follow

  5. git fetch origin, then git merge origin/develop while on develop branch shows:

myMBPro:MyProj user$ git fetch origin myMBPro:MyProj user$ git merge origin/develop Updating 799c6d7a..510c77ab Fast-forward .../Implementations/MyRenderer.cs | 39 ++-- ... etc

  1. I would typically now switch to my branch branch-A and do git merge develop to merge develop to branch-A but I typically do first git status to check all is OK. So, I stay on develop branch and do git status
  2. The problem is that I see git status reporting probably every single file in my project as changed (some as untracked, some as staged and some as ready to be committed).

```

myMBPro:MyProj user$ git status
On branch develop
Your branch is up to date with 'origin/develop'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

, and the list of files ready to commit, files not staged, and files that are untracked follows after this.

So, I end up with lots of files that I haven't even touched, nor modified, nor added now showing as being somehow modified by me. As a result, I am hesitant to merge them in my branch-A.

Any idea why is this happening? Is it even normal (but to me it does not sound normal that files I have not changed appear as they are changed by me and now I need to start tracking them, stage and commit them.

I am on MacBookPro, using git from terminal and also using SourceTree

回答1:

Are you sure that you did git commit immediately before git checkout develop? The only way that I know this can happen if your working directory is dirty before you checkout develop and pull.

Sure, there might be line ending problems, but this still doesn't really explain the issue. An important aspect of git merge is that it must commit. It is guaranteed to commit if there are no conflicts, and the only way it wont commit is if there are merge conflicts (Which can leave you in that state of variously staged and unstaged files). If you aren't getting merge conflicts (Meaning you see the words fast-forward), then there seems to be only one possibility:

Say you're working on branch A. Then you commit. Then, you change a couple of files (Or, they are changed automatically by some software you have running at the same time). Then, when you git checkout develop, you end up copying your index. The index being the set of all of the changes you've made since the most recent commit. You can checkout another branch and your index will follow you. This seems to be the only thing, as far as I'm aware, that could cause what you're experiencing. If the merge affects files that part of your index, then the merge will fail. However, it's possible for the merge to succeed if it's a simple fast-forward that misses your index, which will cause your index to be shown on top of the most recent commit.

For the future, try running git status before git fetch origin; git merge origin/develop. If you're still running into trouble, you might have to copy-paste what's in your terminal so we can read it.

Also, make sure to only have source code and build scripts in the repo, and to .gitignore any files that might be created by vim, your IDE, your build scripts, the running executable, etc. Any of those files are likely going to change constantly.