0

I am trying to add all changed files to GitHub by using git add . but it also adds the changes made by me on other local projects. It should ideally add the changes of the same project.

How should it be solved?

When I run git status then I get to see those extra files which are unstaged. Images:

enter image description here

enter image description here

Nimeshka Srimal
  • 8,012
  • 5
  • 42
  • 57
Shrreya Behll
  • 185
  • 1
  • 10
  • Btw. it would be helpful if you could copy the output from your console and show what you were doing step-by-step. – matb Jul 12 '18 at 17:59
  • This is not possible. I guess you are not running the commands within the right project folder. Can you post your folder structure? – Nimeshka Srimal Jul 12 '18 at 18:24
  • I am running within the correct project folder. I have added the image link as reference – Shrreya Behll Jul 12 '18 at 18:33
  • according to the picture you uploaded, it seems like you have created a git repository inside your home directory. That's wrong. You need to have separate git repositories for every project. – Nimeshka Srimal Jul 12 '18 at 18:55
  • Can you post a screenshot of git status inside the AndroidStudioProjects folder? – Nimeshka Srimal Jul 12 '18 at 19:12
  • I have a folder names AndroidStudioProjects where I save all the projects, isn't that right? – Shrreya Behll Jul 12 '18 at 19:13
  • 2
    Well, @ShrreyaBehll you have a fundamental problem there. You create a separate folder for each project. and in each project folder, you should have independent git repositories. But in your case you have created all these folders inside another git repository. Basically, your home folder is now a git repo. – Nimeshka Srimal Jul 12 '18 at 19:18
  • @ShrreyaBehll have you get the answer which helps you solve the problem? If yes, can you mark the answer by clicking √ symbol on the left of the answer? And it will also benefit others who meet similar questions. – Marina Liu Jul 19 '18 at 08:52
  • @MarinaLiu-MSFT ok! – Shrreya Behll Jul 21 '18 at 09:38

4 Answers4

0

Each repo is in its own directory. Then when you cd myproject and run git add . it should only add files changed in this repo.

matb
  • 851
  • 13
  • 23
0

By Project, if you mean different files directory in the same repo then you should be using one of the below commands to match your needs

For Git version 2.x


  • git add -A stages All

  • git add . stages new and modified, without deleted

  • git add -u stages modified and deleted, without new


Detail:

git add -A is equivalent to git add .; git add -u.

The important point about git add . is that it looks at the working tree and adds all those paths to the staged changes if they are either changed or are new and not ignored, it does not stage any 'rm' actions.

git add -u looks at all the already tracked files and stages the changes to those files if they are different or if they have been removed. It does not add any new files, it only stages changes to already tracked files.

git add -A is a handy shortcut for doing both of those.

You can test the differences out with something like this (note that for Git version 2.x your output for git add . git status will be different):

git init
echo Change me > change-me
echo Delete me > delete-me
git add change-me delete-me
git commit -m initial

echo OK >> change-me
rm delete-me
echo Add me > add-me

git status
# Changed but not updated:
#   modified:   change-me
#   deleted:    delete-me
# Untracked files:
#   add-me

git add .
git status

# Changes to be committed:
#   new file:   add-me
#   modified:   change-me
# Changed but not updated:
#   deleted:    delete-me

git reset

git add -u
git status

# Changes to be committed:
#   modified:   change-me
#   deleted:    delete-me
# Untracked files:
#   add-me

git reset

git add -A
git status

# Changes to be committed:
#   new file:   add-me
#   modified:   change-me
#   deleted:    delete-me
Mukesh A
  • 323
  • 1
  • 4
  • 13
  • Can we change the head of the project manually? What I mean is that we use reset --hard HEAD to move the head to the previous commit ,can we move the head manually forward or any other commit hash? – Shrreya Behll Jul 12 '18 at 18:56
  • I wouldn't advise changing the HEAD per se, but if you want to reset your local branch to a `previous commit` you can do this by `git reset HEAD `. For more details on what `git reset` does and other parameters please refer [this](https://git-scm.com/docs/git-reset#git-reset-emgitresetemltmodegtltcommitgt). And feel free to ask if you have any more queries. – Mukesh A Jul 12 '18 at 19:03
  • OK! Thank you . – Shrreya Behll Jul 12 '18 at 19:18
0

Based on the image you gave, it appears you should be in the Silencio directory. Git is showing you that you have untracked files.

Try this:

cd ~/Silencio
git status

The output will show there are only changes for that directory.

Kristina
  • 1
  • 1
  • 2
  • I am working on a project Example-Api and I am within that path. The issue is the same that it shows changes of the other project named Silencio – Shrreya Behll Jul 12 '18 at 18:54
0

I had a look at the image you uploaded and it seems like you have a weird git setup.

I see that you are inside the folder, ~/AndroidStudioProjects/ApiExample/. So this is your project directory?

But it looks like you have initialized a git repository in your home directory (i.e. ~/) and when you run git status inside the current path, it shows everything relative to that home directory path.

In my opinion, you shouldn't have done that, and should have maintained a separate and an independent git repository for each project.

My suggestion to fix your issue is to remove the .git directory from your home. I guess you don't have any valid projects in the root of your home dir?

  1. First change your path to home directory:

    cd ~

  2. Then take a backup of your .git folder in case you need it.

    cp -r .git .git-backup

  3. Then remove it.

    rm -rf .git

  4. Now go back to your AndroidStudioProjects, there also you shouldn't be having a git repository.

    cd AndroidStudioProjects

  5. Remove the .git folder.

    rm -rf .git

  6. Finally, go to your ApiExample project folder, and initialize a new git repository there.

    git init

Now everything should be fine. But you should know that this is only a suggestion to clear your mess. If you really had a valid git repository inside your home directory, or AndroidStudioProjects directories, then you need to use Git Submodules.

Hope it helps. Feel free to ask any doubts.

Nimeshka Srimal
  • 8,012
  • 5
  • 42
  • 57
  • Thanks for the suggestions, I wished to ask that my folder AndroidStudioProjects in also in the home directory, should that be shifted as well? – Shrreya Behll Jul 12 '18 at 19:38
  • You don't need to shift it. It is upto you that where you keep your project folders. Just try every step i have given and tell me how it goes. – Nimeshka Srimal Jul 12 '18 at 19:48
  • Worth noting @shrreya-behll that if you have any commits in your repo for any of the projects, this will remove your current git history. – matb Jul 12 '18 at 23:30