Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Stop tracking and start ignoring
Vijay Koushik, S. 👨🏽‍💻
Vijay Koushik, S. 👨🏽‍💻

Posted on • Originally published atsvijaykoushik.github.io on

     

Stop tracking and start ignoring

Hello world,

I’ve been playing around with GitHub & Git over the past few months and most of the time I accidentally commit and push files that are totally unrelated to the project. Usually they are the environment files likenode_modules &build directories andpackage.json when using node.js as I use Gulp to minify css & JS and files like_site folder andGemfile.lock when I use Jekyll for my blog.

The reason for this problem? I forget to add the.gitignore file to my repository. The.gitignore file contains all the ignore rules (usually names of files and folders to be ignored) for the project. But there is a catch. The.gitignore file will tell Git to ignore the existence of only those files that haven’t been pushed. Since I have already pushed the files, adding them to the ignore rule won't work. Git will keep tracking the changes to those files and nag me to commit those changes.

I could delete these files and push the deletions to the repo but, they’re going to be recreated every time I build the project. So, that doesn’t work. A quick search on the internet gave me the exact solution I needed. There is a way to remove these files from the repo and keep them locally. By executing the following command in a CLI likegit bash orcmd I deleted the files from the repo without removing them from my local file system.

    git rm --cached <file name>    E.g. git rm --cached package.json

Break down of remove a file only from repository command in gitA break down of the command to remove a file only from the repository

    git rm –r --cached <directory name>    E.g. git rm –r --cached _sites

Break down of remove a folder and it's contents only from repository command in gitA break down of the command to remove a folder and it's contents only from the repository

The--cached flag removes the files from the repository and leaves the local copies undisturbed. And the–r flag recursively removes the files inside the directory specified.

Now that I removed the files from the repo, Git thinks that the local copies of the deleted files are something new I added to the repo. So adding these file names to the.gitignore file will tell git to ignore these files and they won’t be pushed again.

Since this accident happens to me frequently, I decided to post this here so I wouldn't forget to add the ignore rules to the repo. Even if I do forget, I know where to look for the solution.

To make things more simple I found a sitegitignore.io that generates.gitignore file based on the Operating systems, IDEs and programming languages that I provide in an all in one text box.Screenshot of the site gitignore.ioA screenshot of gitignore.io's homepage

Top comments(18)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
tmaila profile image
Tomi Maila
  • Joined

You should not ignore package.json, it is a key file shared between all the developers defining package dependencies. You should ignore build artefacts and installed dependencies and user specific configuration files such as IDE configuration, that is specific to a single user. Note that some IDE config files you don't want to ignore such as build configurations because they're the same for the whole team.

CollapseExpand
 
matteojoliveau profile image
Matteo Joliveau
My name is Matteo and I'm a cloud solution architect and tech enthusiast. In my spare time, I work on open source software as much as I can. I simply enjoy writing software that is actually useful.
  • Location
    Milan, Italy
  • Work
    Software Engineer at Prima
  • Joined

True. AlsoGemfile andGemfile.lock should be included for the same reason.

CollapseExpand
 
svijaykoushik profile image
Vijay Koushik, S. 👨🏽‍💻
Full stack Javascript Developer building complete web application
  • Location
    Madurai, Tamil Nadu, India
  • Education
    Bachelor's degree in Computer Science and Engineering
  • Work
    Javascript Developer at Blaze webservices pvt. ltd.
  • Joined

Wow! Thank you so much for your valuable comments everyone. But, in my defense I want to make it clear why I talked about deletingpackage.json andGemfile.lock files.

  1. I said I deleted thepackage.json file because, The project was my personal static website hosted on GitHub pages. I used gulp as a dev dependency and to me it was never needed to be on the repo since I use a single machine for development.
  2. I talked about me deleting theGemfile.lock file because, my blog is also hosted on GitHub pages and is rendered with Jekyll. So, I do not know the consequences of keeping the file in the repo which is deployed in an environment which I don't have control of.

Thank you all again for pointing out the importance ofpackage.json,package-lock.josn,Gemfile andGemfile.lock files being in the repo 🙂

CollapseExpand
 
writecodeeveryday profile image
Lazaro Herrera
  • Joined

This is true.

package.json should exist in case one of your dependencies in package-lock.json is platform specific and package.json is used for finding a compatible version.

CollapseExpand
 
ferdnyc profile image
Frank Dana
  • Joined
• Edited on• Edited

That's not why this happens. It happens because you're adding those files in the first place. Why are files you don't want to push even being tracked?git add is meant to be a targeted operation, performed only on those files you want in the repo. Don'tgit add * or (worse)git add -A and you won't have this problem.

CollapseExpand
 
ferdnyc profile image
Frank Dana
  • Joined
• Edited on• Edited

(I love.gitignore for its ability to hide things from the "Untracked Files" list ingit status. Because if you're not planning on tracking those files, get them off the list so that git can warn you if you haven't added any that you SHOULD be tracking.)

CollapseExpand
 
svijaykoushik profile image
Vijay Koushik, S. 👨🏽‍💻
Full stack Javascript Developer building complete web application
  • Location
    Madurai, Tamil Nadu, India
  • Education
    Bachelor's degree in Computer Science and Engineering
  • Work
    Javascript Developer at Blaze webservices pvt. ltd.
  • Joined

Thank you for sharing aboutgit add command. Like I said in thiscomment, I use the GitHub Desktop app to commit and push my changes to the remote repo. Since It automatically adds all new files to the tracking list, The issue of committing unwanted files occurs when I'm not vigilant enough 😐.

CollapseExpand
 
tinmanjk profile image
tinmanjk
  • Joined

Thanks for the post and the useful links/images. This particular scenario can really be frustrating :)

Btw have you read a book on git? I found that a good book on git is much more valuable than guides / videos in that scenarios like this one occurred very very, infrequently as a result because of my deeper understanding.

CollapseExpand
 
moopet profile image
Ben Sinclair
I've been a professional C, Perl, PHP and Python developer.I'm an ex-sysadmin from the late 20th century.These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
  • Location
    Scotland
  • Education
    Something something cybernetics
  • Pronouns
    They/them
  • Work
    General-purpose software person
  • Joined
• Edited on• Edited

Rather than have just a project-specific.gitignore, which could contain the world, have one per projectand a global config that includes all the common trash files (like the__MACOSX and.DS_Store files that Macs leave littered around the place, orThumbs.db that Windows generates) and a generic list of IDE trash like.nb_project or whatever.

Some OS and applications are really quite bad citizens but it doesn't need to add commits to your project.

CollapseExpand
 
tinmanjk profile image
tinmanjk
  • Joined

Yep. Sometimes I like some sugar sprinkled over the documentation. I think that's why a book like "You dont know JS" is so popular :)

For git there is a book withactual exercises that helped me solidify the concepts - Git in a Month of Lunches.

CollapseExpand
 
svijaykoushik profile image
Vijay Koushik, S. 👨🏽‍💻
Full stack Javascript Developer building complete web application
  • Location
    Madurai, Tamil Nadu, India
  • Education
    Bachelor's degree in Computer Science and Engineering
  • Work
    Javascript Developer at Blaze webservices pvt. ltd.
  • Joined

You're right!😅 Reading the manual or a book as mentioned by@tinmanjk should've been my first action when I was playing with GitHub repos. But GitHub's easy to use web interface and the GitHub App made me think that I did not need to look into the Git documentation. And thank you for the link to the e-book 👍🏽.

Thread Thread
 
tinmanjk profile image
tinmanjk
  • Joined

You're welcome. I think it's essential to have a safe environment to mess with a new thing in order to learn better the ins and outs. Unfortunately with source control it's generally not advisable to do that :) So here come the exercises, a way to progressively learn.

CollapseExpand
 
alephnaught2tog profile image
Max Cerrina

These graphics are PHENOMENAL!

CollapseExpand
 
svijaykoushik profile image
Vijay Koushik, S. 👨🏽‍💻
Full stack Javascript Developer building complete web application
  • Location
    Madurai, Tamil Nadu, India
  • Education
    Bachelor's degree in Computer Science and Engineering
  • Work
    Javascript Developer at Blaze webservices pvt. ltd.
  • Joined

Thank you so much 😊

CollapseExpand
 
ruffle1986 profile image
Tamas Fodor
  • Location
    Budapest, Hungary
  • Work
    Senior Frontend Engineer
  • Joined

Thank you for sharing!

I agree. It's a very powerful git command every developer should be aware of!

CollapseExpand
 
svijaykoushik profile image
Vijay Koushik, S. 👨🏽‍💻
Full stack Javascript Developer building complete web application
  • Location
    Madurai, Tamil Nadu, India
  • Education
    Bachelor's degree in Computer Science and Engineering
  • Work
    Javascript Developer at Blaze webservices pvt. ltd.
  • Joined

You're welcome :)

CollapseExpand
 
itr13 profile image
Mikael Klages
418 bio is a teapot
  • Joined

I usually get my gitignore templates from here:github.com/github/gitignore

CollapseExpand
 
svijaykoushik profile image
Vijay Koushik, S. 👨🏽‍💻
Full stack Javascript Developer building complete web application
  • Location
    Madurai, Tamil Nadu, India
  • Education
    Bachelor's degree in Computer Science and Engineering
  • Work
    Javascript Developer at Blaze webservices pvt. ltd.
  • Joined

That is one good collection 👍🏽. I actually came across this repository when writing this article. I did not mention this because, in the article I talked about me realizing that I did not add a.gitignore file after making a few commits to the repo and in my perspective it seemed a bit of extra work to select the templates for Operating system, programming language and IDE separately instead of providing the above mentioned as input and get a combined file. Thanks for mentioning this good collection of templates here :)

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Full stack Javascript Developer building complete web application
  • Location
    Madurai, Tamil Nadu, India
  • Education
    Bachelor's degree in Computer Science and Engineering
  • Work
    Javascript Developer at Blaze webservices pvt. ltd.
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp