Git - Create repository and track only new files
Is it possible to initialize a new git repository inside a non-empty directory and track only files/folders newly added after initializing while ignoring the old ones. For example:
Current folder structure before initializing a new repo:
FolderA/ FolderB/ file1.php file2.php
After initializing a new repo and creating new files and direcotries:
FolderA/ // old, ignore FolderB/ // old, ignore FolderC/ // new, track file1.php // old, ignore file2.php // old, ignore file3.php // new, track
Git will only ever track files that you explicitly tell it to using
git add. Therefore, there is nothing you need to do: just don't tell Git to track the files you don't want it to track.
This can get annoying if you look at
git status, which will then list all untracked files.
There is an easy solution for that, too: just ignore all files:
echo '*' > .gitignore
This will tell Git to ignore all files. You can still add individual files to the repository using
git add -f (the
-f is necessary to force Git to ignore the fact that the files are ignored).
If you want to use
git add . or
git add somedirectory to add all new files, then you need to individually list all the files to be ignored in
.gitignore. You can use the "annoying" fact I described above, namely that
git status will list all untracked files:
git status --porcelain -uall | grep -E '^\?\? ' | cut -d ' ' -f2- > .gitignore
--porcelain specifies the output format for
git status to be "Porcelain v1" format. The Porcelain output formats are guaranteed to never change and are designed to be easy to parse by machines, unlike the short or long output formats which are designed to be human-readable and thus may get changed to make them easier to read.
git status to list all untracked files, otherwise it will only list the directory name if all files in a directory are untracked.
grep looks for lines beginning with
?? which is how untracked files are listed. The
cut then cuts out everything after the
??, which is the file name.
Note: this will break with a file named
abc\n?? def.txt, for example. If you have such files, you need to harden the command by e.g. using the
-z flag to
git status which prints the records delimited by ASCII NUL instead of a newline.
Recording Changes to the Repository, In such cases, you can create a file listing patterns to match them named but do track lib.a, even though you're ignoring .a files above !lib.a # only ignore the When starting a new project, one of the first things you'll find yourself needing to do is creating a new Git repository. This not only helps you share the project with coworkers, or publicly, but it's also a great way to track updates to a young project that is bound to go through significant changes in its early life.
Start a new git repository, In such cases, you can create a file listing patterns to match them named do track lib.a, even though you're ignoring .a files above !lib.a # only ignore the root You’ll probably want to create a .gitignore file right away, to indicate all of the files you don’t want to track. Use git add .gitignore, too. Type git commit. Connect it to github. You’ve now got a local git repository. You can use git locally, like that, if you want. But if you want the thing to have a home on github, do the following. Go to github. Log in to your account. Click the new repository button in the top-right. You’ll have an option there to initialize the repository
git init itself can not do that, at least not to my knowledge. But a little shell magic can do the trick:
cd /path/to/directory/ touch .gitignore # creates file .gitignore if not yet existing for file in *; do echo $file >> .gitignore done # appends all the filenames of files in directory to file .gitignore git init # inits the repository
After the repo has been initialized, you still need to add files (including
.gitignore) before committing.
Git: Create a New Repository, Your first instinct, when you start to do something new, should be git init . Say you've just got some data from a collaborator and are about to start exploring it. a .gitignore file right away, to indicate all of the files you don't want to track. Step 3: Initialize Bare Git Repository for the Project. Enter the command git init this command is used to Create Git Repository. Consequently, the execution of this command creates a hidden .git folder therein. Or in other words, an empty Git Repository is initialized.
I ended up with excluding all files/folders and included only specific ones. Here is what I did:
- Created an empty
.gitignorefile within the directory root and added
*to untrack all the files and folders
- Initialized a new repository within that directory
.gitignoreby adding specific files and folders to be tracked from this point onwards
My final .gitignore now uses this pattern:
# Ignore everything * # Include folders !/folderC !/folderD # Include files !file3.php !file4.php # Include files containing !*foo*
This workaround seems to be working fine for now. Thanks to Jörg W Mittag and Michael H. answers i was pointed to the right direction.
There were also some useful resources from other SO users:
Make .gitignore ignore everything except a few files
How do I tell Git to ignore everything except a subdirectory?
git ignore filenames which contain
Git: Add All Files to a Repo, This not only helps you share the project with coworkers, or publicly, but it's also a great way to track updates to a young project that is bound to go through In this article we'll show a few ways to create a new repository. Note that the git add . command will add all files in the current working directory, What is a Git repository? A Git repository is a virtual storage of your project. It allows you to save versions of your code, which you can access when needed. Initializing a new repository: git init. To create a new repo, you'll use the git init command. git init is a one-time command you use during the initial setup of a new repo.
Tracking Files in a Git Repository with "git add", When you want Git to track a file in a repository, you must explicitly add it to use the --ignore-removal option, which will only stage new and modified files: Although this technically isn't adding all files, it's another way to add a batch of files. git add is a multipurpose command — you use it to begin tracking new files, to stage files, and to do other things like marking merge-conflicted files as resolved. It may be helpful to think of it more as “add precisely this content to the next commit” rather than “add this file to the project”.
Adding a file to a repository using the command line, Creating a New Git Repository and Checking its Status In order to start tracking these files, we need to tell git which ones we want to track. We do This time, it takes just a bit longer because it's processing all of the files in this If I were to create a text file inside the Empty directory, Git would pick that up. Step 1: Create a new local Git repository. Open up your terminal and navigate to your projects folder, then run the following command to create a new project folder and navigate into it: mkdir hello-world. cd hello-world. To initialize a new local Git repository we need to run the `git init` command: git init. After you run that command, you should get feedback that an empty Git repository was initialized for your project. Step 2: Adding a new file to our Git repository
First steps with git: clone, add, commit, push, On your computer, move the file you'd like to upload to GitHub into the local directory that was created when you cloned the repository. Open TerminalTerminalGit Note the "Untracked files" message with the file "my_new_file.txt". Git conveniently informs us that we've added a new file to the project. But that isn't enough for Git. As Git tells us, we need to track "my_new_file.txt". In other words, we need to add "my_new_file.txt" to the staging area.