Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add files to .gitignore directly from git shell

I'm trying to add a specific file to .gitignore directly from git shell. File to ignore is token.mat

If I type:

touch .gitignore

and then manually open .gitignore with a text editor and add

token.mat

save and close, it works, git ignores it (when I type git status at the git shell I no longer see token.mat)

However at the git shell if I type:

touch .gitignore
echo 'token.mat' >> .gitignore

then git does not ignore token.mat, when I type git status I still see it. However when I open .gitignore with a text edit I see token.mat listed and it looks exactly the same as when I add it manually.

Does anyone know why this is, or how I can add files to .gitignore directly from the git shell?

Thanks for any help in advance!!

Other details:

  • Windows 7 Professional, service pack 1
  • git version: 2.5.3.windows.1
  • text editor: Notepad ++
like image 644
sjp working Avatar asked Sep 23 '17 19:09

sjp working


People also ask

How do I add files to git ignore?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

Can you add file to Gitignore after commit?

No. Even with an existing . gitignore you are able to stage "ignored" files with the -f (force) flag. If they files are already commited, they don't get removed automatically.


2 Answers

Make sure your echo did not add a trailing space:

echo 'token.mat'>>.gitignore

That way, the .gitignore should work.

>> is for appending to the .gitignore file, while a single > would have overwritten it completely.

Also, 2.5 is ancient: unzip the latest Git, as in PortableGit-2.14.1-64-bit.7z.exe, anywhere you want, add it to your %PATH%, and check again.

like image 161
VonC Avatar answered Oct 29 '22 04:10

VonC


The accepted answer is totally correct, but would be incomplete in day to day scenarios as it only add one file to .gitignore , to add more than one files to your .gitignore

Use the following command :

echo -e 'file1 \n file2 \n file3 \n' >> .gitignore

You can use the above command without touch this will automatically create a .gitignore and add all the files. Make sure that you include -e and >>.

Here -e serves as the flag so the newline character \n is interpreted properly and >> appends the content in the .gitignore file.

If you need to override an already existing .gitignore use > instead of >>

You can learn more about echo by following this link.

like image 33
StabCode Avatar answered Oct 29 '22 03:10

StabCode