167

I'm working first time on git. I have pushed my branch on github and it pushed all the library and documents into the github. Now what can I do and how can I use gitignore command to avoid the same mistake again.

royhowie
  • 11,075
  • 14
  • 50
  • 67
user1559230
  • 2,790
  • 5
  • 26
  • 32
  • 1
    `.gitignore` is a file in your git root directory. Add the name patterns for the files that you want to ignore, and the files will be ignored automatically. – LonelyWebCrawler Sep 19 '12 at 19:12
  • I'm on ubuntu. Can you please say the path of git root directory – user1559230 Sep 19 '12 at 19:15
  • 2
    It is the directory where you used `git init`. The `.git` directory is located there. – LonelyWebCrawler Sep 19 '12 at 19:17
  • 3
    For those who don't kwno how to use gitignore - go to https://www.gitignore.io you can type all the tools you are using (symfony, CakePHP, ZendFramework, wordpress, grunt, sublime text, netbeans, and so on, you get the drill...) and it will generate the git ignore file for you. If you don't know how to use gitignore i think that best way to learn it is to look at some good use case example (like the one generated by the gitignore.io. If you want to use it from cmd line (geeky way) see this Tutorial: https://vimeo.com/70609819 – DevWL Apr 04 '16 at 05:42

8 Answers8

202

So based on what you said, these files are libraries/documentation you don't want to delete but also don't want to push to github. Let say you have your project in folder your_project and a doc directory: your_project/doc.

  1. Remove it from the project directory (without actually deleting it): git rm --cached doc/*
  2. If you don't already have a .gitignore, you can make one right inside of your project folder: project/.gitignore.
  3. Put doc/* in the .gitignore
  4. Stage the file to commit: git add project/.gitignore
  5. Commit: git commit -m "message".
  6. Push your change to github.
Or B
  • 1,675
  • 5
  • 20
  • 41
u19964
  • 3,255
  • 4
  • 21
  • 28
  • can you please tell me how to write the .gitignore file. i.e, how to write the path of the folders and files that I want to ignore inside the .gitignore file. Thanks – user1559230 Sep 20 '12 at 05:12
  • 5
    Hi, the gitignore is just a plain text file. It is actually .gitignore (DOTgitignore). Just put the name of the file/file types you want git to ignore, one per line. Example: http://www.sujee.net/tech/articles/gitignore/ – u19964 Sep 20 '12 at 18:35
  • 1
    ^ http://web.archive.org/web/20120914233504/http://sujee.net/tech/articles/gitignore/ – pulsar Aug 04 '15 at 15:57
66

If you dont have a .gitignore file, first use:

touch .gitignore

then this command to add lines in your gitignore file:

echo 'application/cache' >> .gitignore

Be careful about new lines

Serdar D.
  • 3,055
  • 1
  • 30
  • 23
  • 4
    There is no need to `touch` the file first. `>>` is synonymous to `>` if the file does not exist already. `echo` appends a newline on the end of the output. You can stop that with `-n`. If you need various ignores, either do various echos, or use `-e`, as following: `echo -e "**/cache/\n**.pyc" >> .gitignore`. Double asterisk matches all files digging into folders recursively too. Use `/` on the end of folders, just in case. – mazunki Nov 21 '19 at 03:51
  • 1
    @Andrew tl;dr: Add each ignored file with `echo "file_to_be_ignored" >> .gitignore`, no need to do anything else. End folder names with `/` as `echo "**/modules/" >> .gitignore`. – mazunki May 30 '20 at 11:48
18

git ignore is a convention in git. Setting a file by the name of .gitignore will ignore the files in that directory and deeper directories that match the patterns that the file contains. The most common use is just to have one file like this at the top level. But you can add others deeper in your directory structure to ignore even more patterns or stop ignoring them for that directory and subsequently deeper ones.

Likewise, you can "unignore" certain files in a deeper structure or a specific subset (ie, you ignore *.log but want to still track important.log) by specifying patterns beginning with !. eg:

*.log !important.log

will ignore all log files but will track files named important.log

If you are tracking files you meant to ignore, delete them, add the pattern to you .gitignore file and add all the changes

# delete files that should be ignored, or untrack them with 
# git rm --cached <file list or pattern>

# stage all the changes git commit
git add -A 

from now on your repository will not have them tracked.

If you would like to clean up your history, you can

# if you want to correct the last 10 commits
git rebase -i --preserve-merges HEAD~10 

then mark each commit with e or edit. Save the plan. Now git will replay your history stopping at each commit you marked with e. Here you delete the files you don't want, git add -A and then git rebase --continue until you are done. Your history will be clean. Make sure you tell you coworkers as you will have to force push and they will have to rebase what they didn't push yet.

Orwellophile
  • 13,235
  • 3
  • 69
  • 45
Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
9

There is a file in your git root directory named .gitignore. It's a file, not a command. You just need to insert the names of the files that you want to ignore, and they will automatically be ignored. For example, if you wanted to ignore all emacs autosave files, which end in ~, then you could add this line:

*~

If you want to remove the unwanted files from your branch, you can use git add -A, which "removes files that are no longer in the working tree".

Note: What I called the "git root directory" is simply the directory in which you used git init for the first time. It is also where you can find the .git directory.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
LonelyWebCrawler
  • 2,866
  • 4
  • 37
  • 57
6

If you don't have a .gitignore file. You can create a new one by

touch .gitignore

And you can exclude a folder by entering the below command in the .gitignore file

/folderName

push this file into your git repository so that when a new person clone your project he don't have to add the same again

  • Thanks for suggestion. Should we put '/'? Also "/*" not needed at the end? Is work for Windows and Linux? – Cloud Cho Jun 13 '23 at 21:29
5

There are several ways to use gitignore git

  • specifying by the specific filename. for example, to ignore a file
    called readme.txt, just need to write readme.txt in .gitignore file.
  • you can also write the name of the file extension. For example, to
    ignore all .txt files, write *.txt.
  • you can also ignore a whole folder. for example you want to ignore
    folder named test. Then just write test/ in the file.

just create a .gitignore file and write in whatever you want to ignore a sample gitignore file would be:

# NPM packages folder.
node_modules

# Build files
dist/

# lock files
yarn.lock
package-lock.json

# Logs
logs
*.log
npm-debug.log*

# node-waf configuration
.lock-wscript

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# Jest Coverage
coverage

.history/

You can find more on git documentation gitignore

Afaq Ahmed Khan
  • 2,164
  • 2
  • 29
  • 39
1

on my mac i found this file .gitignore_global ..it was in my home directory hidden so do a ls -altr to see it.

I added eclipse files i wanted git to ignore. the contents looks like this:

 *~
.DS_Store
.project
.settings
.classpath
.metadata
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • when I put the .gitignore file in my project folder it messed up my eclipse project locally. do you know why that could happen and how to prevent it?? for now I'm just staying away from .gitignore. – JesseBoyd Aug 20 '17 at 21:56
  • I don't use eclipse anymore. Inteilji now – j2emanue Aug 21 '17 at 16:02
0

As this link you can add ".gitignore" file to root of your project and add all file or folder name(s) on it. (in my case c# project ignored files and folders)

Note: git ignored this file by defualt.

Ali Rasouli
  • 1,705
  • 18
  • 25