1

Recently I started to work on a big project, I just cloned the repository, the main branch is called dev all the pull requests are merged into that branch.

I haven't done any work on the repo, but for some reason when I do git status it shows changes I don't have any idea. I do this in order to clean my git status:

git add . && git reset --hard HEAD

This way I just remove all those local changes that appeared magically and to make sure my dev branch is up to date with remote I just make a git pull

However this is happening repeatedly, so this is becoming annoying. Today I checked there are some unstaged changes and looking to the remote dev branch I can see that those changes were committed today. I think that for some reason git is synchronizing the dev branch with my local dev branch and showing changes as local changes.

Any idea why this could be happening?

svelandiag
  • 4,231
  • 1
  • 36
  • 72
  • Are you using git from terminal? Is there any other tool involved for git tracking? – Zois Tasoulas May 27 '21 at 03:48
  • @zois Yeah I use terminal, and this is a new laptop – svelandiag May 27 '21 at 03:52
  • Is status clean after the pull? If yes, are you saying that sometime later it's no longer clean and you don't know why? What commands or actions are you doing in between when the status is clean and it's not? – TTT May 27 '21 at 03:54
  • 1
    @TTT correct, that's exactly what is happening. I leave the dev branch clean after the pull. Then I do other things (work on another project or something) and a few hours later I go back to the repo and do a git status and see changes I have not made. – svelandiag May 27 '21 at 03:59
  • 2
    Is your repo on a shared drive such as Dropbox or Google Drive? – matt May 27 '21 at 04:02
  • @matt Not at all, it's on my local hard drive, well I have iCloud integrated but I'm the only one with access – svelandiag May 27 '21 at 04:11
  • Is [git maintenance](https://git-scm.com/docs/git-maintenance) running on your machine? (I doubt it would do what you're describing without custom tasks though.) – TTT May 27 '21 at 04:18
  • @TTT I don't think so, as I can see you need to manually run the `git maintenance` command, and I have never done that in this machine, in fact this is a new laptop. – svelandiag May 27 '21 at 04:36
  • Suppose your `dev` branch has 3 commits on it: A-B-C. And suppose the remote branch has A-B-C-D. One way to achieve what you're witnessing, is: `git reset --hard @{u}` (origin/dev) followed by `git reset C` (note the default reset type when unspecified is --mixed). This will leave you with files in commit D as pending changes, like you described. Though I'm not sure if I can believe that you did that without knowing it, and also did it repeatedly. ;) But maybe knowing the commands that could do it might help, somehow... – TTT May 27 '21 at 04:39
  • Super contrived scenario: what if you forgot to include "--hard" in your command, and instead typed `git reset HEAD` **and** you have a hidden branch actually named HEAD which points to an older commit on `dev`. (I'm not sure if this is even possible, TBH. I'm just brainstorming silly ideas at the moment.) – TTT May 27 '21 at 04:43
  • Note about my previous comment, the command: `git reset --hard @{u}` is the same as `git reset --hard origin/dev`, and in this case is also the same as `git pull`. So any of those, followed by `git reset C` would do it. If somehow HEAD was pointing to C, that would do it too. Admittedly though, as you described, you'd have no reason to actually run that command unless it already happened and you were trying to fix it... – TTT May 27 '21 at 04:56
  • iCloud suggests macOS, which suggests a case-folding file system: if you create a file named `readme` and then try to create a second file named `README`, can do you do that, or does it just re-use the old `readme` file? If the latter, you may run into problems with existing Git repositories that try to use two different files with what your file system thinks of as the "same name". – torek May 27 '21 at 05:05
  • 1
    "I have iCloud integrated" : is the folder with your local git repository synced with iCloud ? – LeGEC May 27 '21 at 09:02
  • @TTT thanks for your ideas, however I cloned the repo few days ago and haven't done any work, and Im sure I use the `--hard` flag because my terminal autocompletes it. – svelandiag May 27 '21 at 19:06
  • @torek I doubt it has something to do with iCloud, as I said before, changes merged in recent pull requests shows as local changes in my repo even before I pull any changes from remote. – svelandiag May 27 '21 at 19:07
  • @LeGEC correct, it's in my Desktop folder and it is sync with icloud – svelandiag May 27 '21 at 19:07
  • 1
    @svelandiag : then remove your repo from sync, and see if it still happens – LeGEC May 27 '21 at 21:06
  • 1
    @LeGEC I think you're right. [Apparently iCloud and Git don't play nice](https://stackoverflow.com/a/55522432/184546). – TTT May 27 '21 at 21:55

1 Answers1

-1

The reason why this is happening is that you're seeing automatically generated files from your IDE.

This is very normal. What you need to do is simply add these files to .gitignore and git add .gitignore && git commit -m "Update gitignore" on dev if you have permissions.

If you don't have permissions, create a branch:

git checkout -b gitignore

add the automatically generate files to .gitignore and commit these changes

git add .gitignore && git commit -m "Update .gitignore"

Once you create another feature branch from dev, just go to that feature branch and

git merge gitignore

However, note that the above solution will only work to files that have not been tracked yet. If your files are tracked, as you did, by staging them, then:

After adding each file to .gitignore, what you need to do is:

git update-index --assume-unchanged <file>

Or else, .gitignore will not be able to ignore these files after they've been tracked.

Alan Deep
  • 2,037
  • 1
  • 14
  • 22
  • 1
    I don't believe this matches the description of the problem: "there are some unstaged changes and looking to the remote dev branch I can see that those changes were committed today." – TTT May 27 '21 at 14:45