Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gemfarmer/192b89cc9e12ce7ee46e49c585c9f133 to your computer and use it in GitHub Desktop.
Save gemfarmer/192b89cc9e12ce7ee46e49c585c9f133 to your computer and use it in GitHub Desktop.
Git: Ignore already checked in file

Git: How can I ignore a file that is already committed to the repo?

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 config.py):

  • Add it to .gitignore:

       $ echo "config.py" >> .gitignore
  • Now tell git to not track this file by removing it from the index:

       $ git rm --cached config.py
  • Doing git status shows you that you have deleted the file. However, if you do ls you will still see the file is still present on your local filesytem.

  • Now, add .gitignore to 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.

Useful Scenario: Managing configuraiton files in Git repositories

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 database.yml to .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 .gitignore

      $ echo "database.yml" >> .gitignore
  • Copy database.yml to 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
  • Doing git status now shows you that you have deleted the file. However, if you do ls you will still see the file is still present on your local filesytem, which is what you need.
  • Now, add .gitignore and database.yml.example to 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.yml to 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

        Note: git stash in above commands will pop database.yml changes to database.yml.example. So, you copy them as database.yml with cp and discard the changes on database.yml.example

    • If they haven't edited the database.yml.
      • This file will be renamed to database.yml.example when they do a git pull. They need to copy this file as database.yml and add their comfiguration to it:

        $ cp database.yml.example database.yml

Note: Make sure you add instructions to the README of your repo informing the new contributer to the database.yml.example as 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment