30

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.

Headshota
  • 21,021
  • 11
  • 61
  • 82

6 Answers6

50

.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.

Ivan Zlatev
  • 13,016
  • 9
  • 41
  • 50
  • 1
    `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! – guapolo Feb 08 '13 at 06:21
  • 1
    @guapolo 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 answer – Ivan Zlatev Feb 08 '13 at 11:01
  • 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. – fanchyna Aug 13 '14 at 15:53
  • @fanchyna Well - that's right. It's even in the documentation at http://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.` – Ivan Zlatev Aug 14 '14 at 16:22
  • 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. – Sindhu S Oct 13 '14 at 08:50
  • 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://stackoverflow.com/a/17195901/3156085 . – vmonteco Apr 08 '20 at 04:27
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.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
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.

Derick Schoonbee
  • 2,971
  • 1
  • 23
  • 39
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.

Victor Zamanian
  • 3,100
  • 24
  • 31
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
CbIpHuK
  • 26
  • 4
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

Soheil Hosseini
  • 422
  • 6
  • 10