Git ignore particular files
Asked Answered
S

6

29

I've been playing around with it for a while and I like it very much. Let me describe my case.

I have two computers where I have my repositories and a remote repository.

in my files I have one configuration file which differs on both computers. so when I do pull on my computers I don't need that config file to be pulled, but everything else. How can I achieve that?

I've read about .gitignore files but I can't figure out how they work or are they the thing that I need in my case.

Splendent answered 8/4, 2011 at 12:56 Comment(0)
K
49

.gitignore is for files/folders that you haven't checked in already. It's essentially a text file that you create in the repository and has a list of paths relative to the directory it was placed in, which Git will ignore. You can check-in the .gitignore file as part of the repository. you can find examples - at the end of this page

However if your file has already been checked in the repository then you can use:

git update-index --assume-unchanged file

which will tell Git to ignore any changes to that file made in the future. However this is a local configuration so you will have to do it on each machine you check out. You can revert it by doing:

git update-index --no-assume-unchanged file

Depending on what configuration that file contains it might be a good practice to have a skeleton/example config file in the repository - similar to what PHP guys do with config_example.php, which then is used by people to create config.php, which in turn never get's checked in the repository because it is ignored.

Kronick answered 8/4, 2011 at 13:8 Comment(6)
git update-index --assume-unchanged worked for me (no hyphen after git). Using git version 1.8.1.2 - OS X 10.8.2. Thanks!Tyrannicide
@Tyrannicide In the past most git commands were in the form of git-COMMAND rather than git COMMAND that's why there is a hyphen in the answerKronick
but what is the file is already been tracked. I did your git command with --assume-unchanged option, and then tried to pull the master, it still gives me an error message: error: Your local changes to 'conf.py' would be overwritten by merge. Aborting.Silicious
@Silicious Well - that's right. It's even in the documentation at git-scm.com/docs/git-update-index This option can be also used as a coarse file-level mechanism to ignore uncommitted changes in tracked files (akin to what .gitignore does for untracked files). Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.Kronick
Will this work in a case where I am trying to update local copy by doing git pull --rebase origin master? I tried it out and it didn't. I wanted it to ignore a file while pulling from a remote.Meagan
It was useful for me. But I'd like to add that the files you mark this way are then displayed on a h headed line with a git ls-files. Hence you can list them with a git ls-files | grep '^h' as stated here : https://mcmap.net/q/63697/-undo-git-update-index-assume-unchanged-lt-file-gt .Disadvantage
C
8

If you want to ignore file config.local in your repository, you just create a .gitignore file with /config.local line in it, add it and commit to the repository. That's it.

Chape answered 8/4, 2011 at 13:1 Comment(0)
S
3

Maybe an example will help. Here is a section of my .gitignore file that is also part of the project files:

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
*.pro.user.*
tmp/
release/
debug/
bin/
moc_*
ui_*
*.bck
*_debug

#Visual Studio stuff
lib/*.lib
*.ncb
*.suo

This is just a part of the file. You'll need to tune yours depending on your project.

The point is that the format is flexible and you should not have issues sharing the file. If you have issues, exclude it from the repository.

Smallclothes answered 8/4, 2011 at 13:7 Comment(5)
do I need to place it in root folder?Splendent
I normally keep one in the root. And then create special ones in a folder for special cases that I do not want to put into the "master .gitignore" file.Smallclothes
I've added ignore file, but when I change the config file, in status it still shows that file as modified. is it how it should be?Splendent
Has it been already added? What does git status say exactly - just modified?Smallclothes
Oh, finally it works, I removed it from repo and it's now ignored. ThanksSplendent
O
2

The .gitignore file might be what you need. In that file, you list file patterns -- one per line -- which will cause git status not to report on those files.

But if you have already committed those files, they will be pulled when you do a git pull.

You can also commit the .gitignore file to the repository. That way the same files will be ignored on all computers.

Ockham answered 8/4, 2011 at 13:1 Comment(2)
Note on committing .gitignore: if you're not planning to commit it, it's better to use .git/info/exclude instead.Chape
Thanks for that complementary comment!Ockham
F
0

Please also note that

git update-index --assume-unchanged file

will not help if you try to switch branch

$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
    file
Please commit your changes or stash them before you switch branches.
Aborting
Famulus answered 5/6, 2017 at 18:48 Comment(0)
H
0

If a file is already tracked by Git, .gitignore doesn't apply. Git will continue to track changes to that file. Add the file in your .gitignore. Run the following command: git rm --cached Commit the removal of the file and the updated .gitignore to your repo.

https://learn.microsoft.com/en-us/azure/devops/repos/git/ignore-files?view=azure-devops&tabs=visual-studio

Harmonica answered 25/8, 2020 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.