0

I have a folder that I want to create a new git repository and have only specific files added to it via git add.

However, when I do git init with the following code, EVERYTHING ends up in my remote git repo.

My folder/contents already exist, and I want to just add the files/tasks/vars folders only.

I saw there was another post asking a similar question, but was using git-python commands, Id prefer to use just native "git" commands

Here is my code:

git init
git config user.email user_name@mail_server.com
git config user.name user_name
git checkout -b main
git remote add origin {{ created_gitlab_repo.project.ssh_url_to_repo }}
git add files/
git add tasks/
git add vars/
git commit -m "Initial commit"
git push -u origin main

Is there not a way to add only what I need?

The work around I guess is I just delete the folders I dont want prior to git init?

ZZTest
  • 89
  • 7
  • `git init` doesn’t add anything to the repo. That’s what `git add` does. You can be as broad or specific as you want. Where are you creating the repo? – evolutionxbox Feb 17 '22 at 15:51
  • 3
    Probably just need a `.gitignore` file – Jonathan.Brink Feb 17 '22 at 15:51
  • 1
    Try running `git status` between your commands and it will help you to see which one is adding the files/folders you don't want – Lucas Campos Feb 17 '22 at 15:53
  • 1
    Simply put a .gitignore file in your root directory at some point before your `git add` commands. Then this question sort of becomes this: [What is .gitignore exactly?](https://stackoverflow.com/questions/27850222/what-is-gitignore-exactly) – TTT Feb 17 '22 at 16:16
  • 2
    Or, this might be more targeted to your scenario: [Make .gitignore ignore everything except a few files](https://stackoverflow.com/q/987142/184546) – TTT Feb 17 '22 at 16:22
  • 1
    Unclear what the issue is. Show us what is wrong with what ends up on the remote. – matt Feb 18 '22 at 14:12

1 Answers1

2

A few assumptions:

  • the local git repository is brand new, this is, prior the git init there is not a root .git folder,
  • besides the mentioned selected folders to add to the git repo you may have, or not, other files and folders, the ones you don't want to add to the git repo,
  • these commands are not within a script where you're trying to automate an operation, so you can "monitor" after some of these commands what files you are staging hence committing prior to sending to remote.

Assuming your working folder looks something like this:

working-folder/
|-file-01.ext
|-file-nn.ext
|-files/
  |-file-01.ext
  |-file-nn.ext
|-tasks/
  |-file-01.ext
  |-file-nn.ext
|-vars/
  |-file-01.ext
  |-file-nn.ext
|-other-folder-01/
  |-file-nn.ext

You would be needing a .gitignore file with something like this, for clarity:

/*
!files/*
!tasks/*
!vars/*

If this is a common folder structure you work with frequently, instead of doing git add for every selected folder, and with a propper .gitignore file, you'll only need a git add . at the top level of your working folder.

After every git add ... you could git status to make sure everything you are looking for is going the way you want.

ecedenyo
  • 174
  • 1
  • 8