user1559230
user1559230

Reputation: 2811

How to use gitignore command in git

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

Upvotes: 171

Views: 445633

Answers (10)

Elmar
Elmar

Reputation: 4407

The cleanest way, do it directly from the terminal or command prompt like below.

For files (Linux or Mac Terminal):

echo "you_file_path" >> .gitignore

For files (Windows Command Prompt):

echo .rx_env >> .gitignore

For files (Windows PowerShell):

Add-Content .gitignore "you_file_path"

Upvotes: 0

Dusty Shaw
Dusty Shaw

Reputation: 64

For running dotnet apps, open the terminal in Visual Studio or VS Code and enter dotnet new gitignore

Upvotes: 0

Ali Rasouli
Ali Rasouli

Reputation: 1901

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.

Upvotes: 0

Afaq Ahmed Khan
Afaq Ahmed Khan

Reputation: 2302

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

Upvotes: 5

karthik vishnu kumar
karthik vishnu kumar

Reputation: 1021

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

Upvotes: 6

Serdar D.
Serdar D.

Reputation: 3391

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

Upvotes: 70

Adam Dymitruk
Adam Dymitruk

Reputation: 129584

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.

Upvotes: 20

u19964
u19964

Reputation: 3345

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.

Upvotes: 211

j2emanue
j2emanue

Reputation: 62519

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

Upvotes: 1

LonelyWebCrawler
LonelyWebCrawler

Reputation: 2906

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.

Upvotes: 9

Related Questions