Did you ever accidentally push the compiled files( like .pyc files), log files or the executable files(.exe files) or files( such as settings.py ) that you never intend to ?
If yes, then here’s the solution to your problem.
Just ignore the files you do not ever want to push. Hey ! hey ! this isn’t a normal ignore. What actually comes to your rescue is ‘gitignore’.
Creating a .gitignore file
If you create a file in your repository named .gitignore, Git uses it to determine which files and directories to ignore, before you make a commit.
A .gitignore file should be committed into your repository, in order to share the ignore rules with any other users that clone the repository.
- In Terminal, navigate to the location of your Git repository.
~$ touch .gitignore
This creats a .gitignore file.
Now that you have created a .gitignore file, the next step is to put contents in it. We create .gitignore file listing patterns to match the file names you want to ignore.
Here is an example of .gitignore file:
# this is my first .gitignore file *.pyc *~ *.[oa] settings.py
- The first line starts with # and these are ignored.
- The second line tells Git to ignore any files ending in .pyc.
- The third line tells Git to ignore all files that end with a tilde (~), which is used by many text editors such as Emacs to mark temporary files.
- The fourth line tells git to ignore any files ending in .o or .a — object and archive files that may be the product of building your code.
- The fifth line tells to ignore the file named ‘settings.py’.
- You may also include a log, tmp, or pid directory, automatically generated documentation, and so on.
The rules for the patterns you can put in the .gitignore file are as follows:
• Blank lines or lines starting with # are ignored.
• Standard glob patterns work.
• You can end patterns with a forward slash (/) to specify a directory.
• You can negate a pattern by starting it with an exclamation point (!).
Glob patterns are like simplified regular expressions that shells use. An asterisk (*) matches zero or more characters; [abc] matches any character inside the brackets (in this case a, b, or c); a question mark (?) matches a single character; and brackets enclosing characters separated by a hyphen([0-9]) matches any character between them (in this case 0 through 9).
Here is another example of a .gitignore file:
# a comment - this is ignored *.a # no .a files !lib.a # but do track lib.a, even though you're ignoring .a files above /TODO # only ignore the root TODO file, not subdir/TODO build/ # ignore all files in the build/ directory doc/*.txt # ignore doc/notes.txt, but not doc/server/arch.txt
Note that, each line in gitignore file specifies a pattern or a filename ( if that is to be ignored everytime ).
If you already have a file checked in, and you want to ignore it, Git will not ignore the file if you add a rule later. In those cases, you must untrack the file first, by running the following command in your terminal:
~$ git rm --cached
Setting up a .gitignore file before you get going is generally a good idea so you don’t accidentally commit files that you really don’t want in your Git repository.So, Keep ignoring or, particularly, Keep ‘git-ignoring’. 😉 😛