Showing posts with label GIT Ignore. Show all posts
Showing posts with label GIT Ignore. Show all posts

Friday, 28 December 2018

GIT: How can I ignore files that have already been committed to the repo?

Git can only ignore files that are untracked - files that haven't been committed to the repository, yet. That's why, when you create a new repository, you should also create a .gitignore file with all the file patterns you want to ignore.
However, of course, not everything goes perfect... and files slip through that you later would like to see ignored.

Preparing the Cleanup

Before cleaning up your repository, you should conduct two important preparations:
  1. Make sure your .gitignore file is up-to-date and contains all the correct patternsyou want to ignore.
  2. Commit or stash any outstanding local changes you might have. Your working copy should be clean before you continue.
TIP

How to Ignore Files

Our free online book explains the general process of how to ignore files in great detail.

Cleaning Ignored Files

In three steps, you can clean up your repository and make sure your ignored items are indeed ignored:
$ git rm -r --cached .
$ git add .
$ git commit -m "Clean up ignored files"

GIT: Starting with an Unversioned Project

Let's start with an existing project that is not yet under version control. Change into the project's root folder on the command line and use the "git init" command to start versioning this project:
$ cd path/to/project/folder
$ git init
Now take a moment to look at the files in that directory (including any hidden files):
$ ls -la
You'll see that a new, hidden folder was added, named ".git". All that happened is that Git created an empty local repository for us. Please mind the word "empty": Git did notadd the current content of your working copy as something like an "initial version". The repository contains not a single version of your project, yet.
GLOSSARY

Working Copy

The root folder of your project is often called the "working copy" (or "working directory"). It's the directory on your local computer that contains your project's files.

You can always ask the version control system to populate your working copy with any version of your project. But you always only have one working copy with one specific version on your disk - not multiple in parallel.

Ignoring Files

Typically, in every project and on every platform, there are a couple of files that you don't want to be version controlled: on Mac OS, e.g., those pesky ".DS_Store" files aren't worth versioning. In other projects, you might have build or cache files that make no sense in a version control system. You'll have to decide yourself which files you don't want to include.
NOTE

Which Files Should I Ignore?

As a simple rule of thumb you'll most likely want to ignore files that were created automatically (as a "by-product"): temporary files, logs, cache files...
Other examples for excluded files range from compiled sources to files that contain passwords or personal configurations.
A helpful compilation of ignore rules for different projects and platforms can be found here: github.com/github/gitignore
The list of files to ignore is kept in a simple file called ".gitignore" in the root folder of your project. It's highly recommended to define this list at the very beginning of your project - before making your first commit. Because once files are committed, you'll have to jump through some hoops to get them out of version control, again.
Now, let's get going: Create an empty file in your favorite editor and save it as ".gitignore" in your project's root folder. If you're on a Mac, e.g., you'll want to make sure it contains at least the following line:
.DS_Store
If there are other files you want to ignore, simply add a line for each one. Defining these rules can get quite complex. Therefore, to keep things simple, I'll list the most useful patterns which you can easily adapt to your own needs:
  • Ignore one specific file: Provide the full path to the file, seen from the root folder of your project.
    path/to/file.ext
  • Ignore all files with a certain name (anywhere in the project): Just write down the file's name, without giving a path.
    filename.ext
  • Ignore all files of a certain type (anywhere in the project):
    *.ext
  • Ignore all files in a certain folder:
    path/to/folder/*

Making Your First Commit

With some ignore rules in place, it's time to make our initial commit for this project. We'll go into great detail about the whole process of committing a little later in this book. For now, simply execute the following commands:
$ git add -A
$ git commit -m "Initial commit"

Thursday, 27 December 2018

GIT: Configure files and directories to ignore


8.1. Ignoring files and directories with a .gitignore file

Git can be configured to ignore certain files and directories for repository operations. This is configured via one or several .gitignore files. Typically, this file is located at the root of your Git repository but it can also be located in sub-directories. In the second case the defined rules are only valid for the sub-directory and below.
You can use certain wildcards in this file. * matches several characters. 
For example, the following .gitignore file tells Git to ignore the bin and target directories and all files ending with a ~.
# ignore all bin directories
# matches "bin" in any subfolder
bin/

# ignore all target directories
target/

# ignore all files ending with ~
*~
You can create the .gitignore file in the root directory of the working tree to make it specific for the Git repository.
The .gitignore file tells Git to ignore the specified files in Git commands. You can still add ignored files to the staging area of the Git repository by using the --force parameter, i.e. with the git add --force [paths] command.
This is useful if you want to add, for example, auto-generated binaries, but you need to have a fine control about the version which is added and want to exclude them from the normal workflow.
It is good practice to commit the local .gitignore file into the Git repository so that everyone who clones this repository have it.

8.2. Stop tracking files based on the .gitignore file

Files that are tracked by Git are not automatically removed if you add them to a .gitignore file. Git never ignores files which are already tracked, so changes in the .gitignore file only affect new files. If you want to ignore files which are already tracked you need to explicitly remove them.
The following command demonstrates how to remove the .metadata directory and the doNotTrackFile.txt file from being tracked. This is example code, as you did not commit the corresponding files in your example, the command will not work in your Git repository.
# remove directory .metadata from git repo
git rm -r --cached .metadata
# remove file test.txt from repo
git rm --cached doNotTrackFile.txt
Adding a file to the .gitignore file does not remove the file from the repository history. If the file should also be removed from the history, have a look at the git filter-branch command which allows you to rewrite the commit history.

8.3. Global (cross-repository) .gitignore settings

You can also setup a global .gitignore file valid for all Git repositories via the core.excludesfile setting. The setup of this setting is demonstrated in the following code snippet.
# Create a ~/.gitignore in your user directory
cd ~/
touch .gitignore

# Exclude bin and .metadata directories
echo "bin" >> .gitignore
echo ".metadata" >> .gitignore
echo "*~" >> .gitignore
echo "target/" >> .gitignore
# for Mac
echo ".DS_Store" >> .gitignore
echo "._*" >> .gitignore

# Configure Git to use this file
# as global .gitignore

git config --global core.excludesfile ~/.gitignore
The global .gitignore file is only locally available.

8.4. Local per-repository ignore rules

You can also create local per-repository rules by editing the .git/info/exclude file in your repository. These rules are not committed with the repository so they are not shared with others.
This allows you to exclude, for example, locally generated files.

8.5. Tracking empty directories with Git

Git ignores empty directories, i.e., it does not put them under version control. If you want to track an empty directory in your Git repository, it is a good practice to put a file called .gitignore in the directory. As the directory now contains a file, Git includes it into its version control mechanism.
The file could be called anything. Some people suggest to call the file .gitkeep. One problem with this approach is that .gitkeep is unlikely to be ignored by build systems. This may result in the .gitkeepfile being copied to the output repository, which is typically not desired.