2

I need to work on file.txt locally.

I clone a project is in github and i modify this file.txt locally.

When i do

git checkout .
git pull --no-edit

file.txt is changed because remote change it.

How i can ignore file.txt is not update locally.

File is in this directory, a/file.txt. I tried to add this in .gitignore or .git/info/exclude but always remote updates this files overwrite my file locally.

How i can ignore files not being modified locally, remote always updates .gitignore and a/file.txt

Kurogane
  • 31
  • 2
  • 1
    That's not what .gitignore is for. Try stash, pull and unstash to retain your changes. You will however have to deal with conflicts. – Jan Wilamowski Jul 06 '21 at 03:31
  • 1
    "file.txt is changed because remote change it" No, file.txt is changed because you said `git checkout .` — why would you ever say that? Basically that command means "please destroy all my current work". You can hardly be surprised when that is just what happens. – matt Jul 06 '21 at 03:56
  • Then how suppose to get updates? i want to update repo but only two files not update. – Kurogane Jul 06 '21 at 04:02
  • Well, that's what the answers are trying to tell you. – matt Jul 06 '21 at 04:04

4 Answers4

1

The problem is i want to update others files only these two files not

Yes, I have configuration files that I feel the same way about. But there is no simple automatic solution. Basically if I wanted to keep my local version of a file a.txt while updating everything else from the remote, I would say

git stash

then I would

git fetch
git merge 

and then I would restore the stashed version of the file:

git restore --source stash@{0} -- a.txt
matt
  • 515,959
  • 87
  • 875
  • 1,141
0

Try without git checkout as this is likely where you are losing your work, checking out the file to the state of your local HEAD. Instead just use git pull

If the file in the repo has been updated by someone else, you should be merging their changes in right?

user25794
  • 716
  • 7
  • 14
  • The problem is i want to update others files only these two files not. Not understand last part, i only want my files not being updated for remote and i not want update my files to remote. – Kurogane Jul 06 '21 at 03:51
0

If the file is already pushed to the remote repository (and it obviously is, because you pull and get updates on is), then .gitignore won't help. .gitignore is intended for the cases where you don't want to even commit the file to git in the first place.

What you describe is something that goes against git's intuition.

So you pull from the remote repo, and someone else changes the file - great, this means that there was "some work done" on this file. Git, being a source code management system, is perfectly fine with that. Now you say that meanwhile you've also changed the file. But now what do you intend to do with this change? If you want to commit this change a part of you work, then you have a conflict - something that you should resolve by yourself, so that the file will include both changes done by other people and your changes.

If you don't want to commit the file, then what's the point? Of course you can copy it aside, pull everything and "paste" the file, at the end git kind of assumes that you will keep your changes in git.

Now, having said that, you can "force" git to deal with conlicts in a way that it will always favor your changes over those done in remote repo. You can specify a merge strategy during the git pull operation as a parameter, probably you're interested in a strategy called "ours".

You can read about merge strategies Here

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • I think all misunderstand what i want because i'm noob in git. I tried to explain better. I clone the repo in my computer but i modify two files locally, because this repo have updates i want that updates but in these updates, updated the files i modify so i want to prevent that is not give me these updates. I only want to keep this in my computer not commit or whatever is called to remote. – Kurogane Jul 06 '21 at 04:11
  • In this case - read about merge strategies - the link that I've posted :) – Mark Bramnik Jul 06 '21 at 04:19
0

Without doing the git checkout ., you can:

  • make sure file.txt remains ignored locally
  • pull

See "Git - Difference Between 'assume-unchanged' and 'skip-worktree'"

Try:

git update-index --skip-worktree -- file.txt
git pull

file.txt will not be updated by git pull.

Once you are done with file.txt:

git update-index --no-skip-worktree -- file.txt
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250