Kurogane
Kurogane

Reputation: 31

How to ignore file in git

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

Upvotes: 2

Views: 644

Answers (4)

VonC
VonC

Reputation: 1323963

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

Upvotes: 0

matt
matt

Reputation: 535086

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

Upvotes: 1

Mark Bramnik
Mark Bramnik

Reputation: 42451

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

Upvotes: 0

user25794
user25794

Reputation: 775

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?

Upvotes: 0

Related Questions