git

Remove committed file from a repository

raulmonteroc

Raul Montero

Posted on October 31, 2019

Remove committed file from a repository

No matter what kind of project you are working on, they all have auto-generated files that don’t need to go inside the remote repository, but more often than not, we find ourselves working on projects that have those files already pushed and end up having to deal with conflicts that could have been avoided otherwise.

Thankfully, there is a way to remove those files and make sure no other developer commits them ever again.

1. Remove the file locally

git rm -r --cached route/to/file.cs

2. Prevent the file to be added ever again by adding it to the .gitignore definition.

  • Open the terminal and navigate to the root of your project folder
  • Enter open .gitignore to open the gitignore file in your default text editor. If you don’t have one you can find one configured for your specific project in gitignore.io.
  • Add the relative path of the file you wish to ignore. Here’s a sample ignoring the AndroidResource.designer.cs of a xamarin project
# http://Xamarin.Android Resource.Designer.cs files

**/*.Android/**/[Rr]esource.[Dd]esigner.cs
**/*.Droid/**/[Rr]esource.[Dd]esigner.cs
**/Android/**/[Rr]esource.[Dd]esigner.cs
**/Droid/**/[Rr]esource.[Dd]esigner.cs

3. Stash changed files

git add route/to/file.cs && git add .gitignore

4. Commit changes

git commit -m "Removed auto-generated [file] from repository"

5. Push changes

git push origin master
💖 💪 🙅 🚩
raulmonteroc
Raul Montero

Posted on October 31, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related