You have a repo and quite some developers have cloned the repo and working on it. Now you want to add a file to gitignore which is already checked-in or tracked by Git.(The file is already commited into the repo)
Below are the steps on how to ignore this file (lets say the filename is
Add it to
$ echo "config.py" >> .gitignore
Now tell git to not track this file by removing it from the index:
$ git rm --cached config.py
git statusshows you that you have deleted the file. However, if you do
lsyou will still see the file is still present on your local filesytem.
.gitignoreto index and commit:
$ git add .gitignore $ git commit -m "Ignore config.py"
Note: When other developers pull this commit, the file
config.py will be deleted from their local filesystem.
Its very common that different developer machines require different configurations. Lets say we have a
database.yml file which specifies the database configuration.
Lets assume, you thought you would be the only one working on the repo and checked-in this file to git with your local configuration.
Now you have more developers working on it and very now-and-then some of them commits their local configuration to this file and that breaks everyone's development environment. How do you manage this?
Simple! The common practice is to add
.gitignore and not commit it. Instead, copy it to
database.yml.example with dummy configuration and commit that. Add in the
README of the project telling the developers to make a
database.yml file from the
database.yml.example to configure their local environment.
This is great when you start a new repo but you already have a repo and already checked-in this
database.yml file. By doing the following, you could achieve what is described above.
First, add the file to
$ echo "database.yml" >> .gitignore
database.yml.example(be sure to remove any sensitive information like passwords):
$ cp database.yml database.yml.example
- Now tell git to not track this file by removing it from the index:
$ git rm --cached database.yml
git statusnow shows you that you have deleted the file. However, if you do
lsyou will still see the file is still present on your local filesytem, which is what you need.
- Now, add
database.yml.exampleto index and commit:
$ git add .gitignore database.yml.example $ git commit -m "Ignored database.yml and added database.yml.example"
- Push this to the remote and inform the others who also work on this repo about the following:
- If they have already edited
database.ymlto match their configuration.
when they pull, the pull fails with the message
error: Your local changes to the following files would be overwritten by merge: database.yml. They need to do the following:
$ git stash $ git pull origin <branch_name> $ git stash pop $ cp database.yml.example database.yml $ git checkout database.yml.example
git stashin above commands will pop
database.yml.example. So, you copy them as
cpand discard the changes on
- If they haven't edited the
This file will be renamed to
database.yml.examplewhen they do a
git pull. They need to copy this file as
database.ymland add their comfiguration to it:
$ cp database.yml.example database.yml
- If they have already edited
Note: Make sure you add instructions to the
README of your repo informing the new contributer to the
database.yml to match their configuration.
That's it! Developers can change
database.yml to match their configuration as they want and this will not effects others.