0

i was working on a branch for some changes on a files called S3Trait.php , and next they when i checked the branch all my changes were gone

not sure why but luckily my ide has history and i could see files been changed externally (maybe i did a pull from origin or something ).... no problem i reverted the changes back using ide history

but now something odd is happening , when i switch to other branches i can see the reverted files mentioned

$ git checkout dev
Switched to branch 'dev'
M       app/Traits/S3Trait.php
Your branch is up to date with 'origin/dev'.


git checkout main
Switched to branch 'main'
M       app/Traits/S3Trait.php
Your branch is behind 'origin/main' by 12 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)

and when i do a git status this files shows up on all branches as modified

$ git status
On branch main
Your branch is behind 'origin/main' by 12 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   app/Traits/S3Trait.php

here is dev

$ git status
On branch dev
Your branch is up to date with 'origin/dev'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   app/Traits/S3Trait.php

it's like this file has been modified across all branches ... and if i restore it in one branch i will lose the change in all branches

(dev)
$ git restore app/Traits/S3Trait.php

(dev)
$ git checkout main
Switched to branch 'main'
Your branch is behind 'origin/main' by 12 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

(main)
$ git status
On branch main
Your branch is behind 'origin/main' by 12 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

nothing to commit, working tree clean

not sure what's happening !

phd
  • 82,685
  • 13
  • 120
  • 165
hretic
  • 999
  • 9
  • 36
  • 78
  • 1
    https://stackoverflow.com/q/246275/7976758 "*The key to remember is that the file was **not** modified in the … branch. It was only modified in your working copy. Only when you commit are the changes put back into whichever branch you have checked out*" (C) Gareth. – phd Jul 02 '23 at 11:12
  • Git traffics in _commits_ and that is _all_ it traffics in. Whatever you do that is not a commit happens behind Git's back and is not Git's responsibility. – matt Jul 02 '23 at 14:03

2 Answers2

1

Apparently, you have reverted that file on disk but haven't committed the changes in git.

Switch to the branch you expect and add + commit that file :

git switch <branch>
git add app/Traits/S3Trait.php
git commit

If you want to have the same content on several branches, you will need to run the same steps on all branches, or to merge the changes from your first branch.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
0

Aparently your local repository has been damaged. If you have no unpushed commits in other branches, then I advise you to clone your repository again while keeping the old clone intact for the time being, switch to your branch and paste the new version of the file.

If you have some unpushed commits in other branches, well, you will need to watch your old copy of the repository for the contents of those commits and apply them in your new copy.

Once all the changes you need are already in place in your new copy, you can remove the old, damaged copy.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175