0

I cloned a repo and made few changes to the files that belong only to my dev environment. So, I don't want these files to ever be added or pushed. I will be making several changes to various other files that I do want to be pushed.

How should I handle this case?

Should I add these files in gitignore? Usually I create a new branch for all my changes and then do git add .. But in this case that would mean that all files would get checked in.

birdy
  • 9,286
  • 24
  • 107
  • 171

2 Answers2

0

I see 2 options to this scenario

Option 1 - Hide your changes by using .gitignore

I would use this option if you don't have too many changes to make, but even then you may need to deal with the issue of not changing the repo's .gitignore file, as discussed here:
How to ignore files only locally in git?

Option 2 - create a 2nd, local, "pristine" repo

If you are working on a non-trivial app, (involving a DB connections, passwords, debugging etc) and want to work freely without worrying about sending extraneous code to the upstream repo, you can create a 2nd local repo, whose only purpose will be to hold the commit snapshots you want to send to the main repo.
create 2nd "pristine" local repo

git clone https://github.com/user_name/project_name.git pristine/

Once you are ready to send work to the main upstream or origin, you can add the development/dirty repo as a remote to the pristine and fetch work from it.
git cherrypick could be useful to help get things organised.

(From the pristine repo folder) add the "dirty" repo as a remote:

git remote add dirty file:///path/to/your/repo

fetch the work you want to push

git fetch dirty/<branch>

(Once you are happy with changes) push to main upstream

git push origin
Community
  • 1
  • 1
Michael Coleman
  • 3,288
  • 3
  • 19
  • 18
-1

Yup you can add these files to your .gitignore. That's pretty straightforward, you just add the filename/path in the .gitignore and it will be handled for you.

The other thing you can do is instead of doing git add -A or git add . is that you can do git add -i.

This useful command gives you a menu that specifically lets you tailor what you want to push to git/track before adding any files. More information on how to do that is available here under "interactive mode": http://git-scm.com/docs/git-add

But if you never want to track those files, it's better to put them in your .gitignore than having to check what you are/aren't tracking each time you commit.

flareartist
  • 784
  • 1
  • 6
  • 11
  • This is only for his box, though. It doesn't make sense to add them to everyone's `.gitignore`. – Makoto Jan 30 '15 at 02:21
  • Well it depends on the situation, and there is a local .gitignore. I didn't tell him to add it to everyone's – flareartist Jan 30 '15 at 02:23
  • How so? `.gitignore` is committed to the repository, which means everyone has access to it. You *could* configure a global `.gitignore` that reads from a similarly named but differently located file, but there's already a mechanism that you could use to accomplish the same thing. – Makoto Jan 30 '15 at 02:24
  • Ah, okay I see what you're saying. But chances are if there's a reason he doesn't want to push these files, then others shouldn't want to push them as well. It depends on what the situation is. We're both just making different assumptions. But if you're correct and others want to see those files, putting them in the .gitignore wouldn't be right, you're right. But if those files should never be tracked (how I interpreted it) putting them in the .gitignore would be best http://git-scm.com/docs/gitignore – flareartist Jan 30 '15 at 02:38
  • [You should probably read this.](http://stackoverflow.com/q/1753070/1079354) It's what I was getting at; a way to ignore files locally without putting them in the global `.gitignore`. – Makoto Jan 30 '15 at 02:40