3

Okay, This might be a silly question, but I am new to git, and getting confused a lot.

I have made some files on IntelliJ and now when I run git status in my IntelliJ terminal I get this:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)

  new file:  file x
  new file:  file y
  new file:  file z

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)

 Modified: file x
 Modified: file y
 Modified: file z

Untracked files:
  (use "git add <file>..." to include in what will be committed) 

  file a 
  file b

I want to stage and commit the file x, file y, file z and do not want to stage the file a and file b.

I know git add . will stage all tracked and untracked files. Can something like git add -u do the job?

Note: There are a lot of files and I don't want to individually add them all.

Aditya Verma
  • 428
  • 6
  • 22
  • 1
    `git add -u` does what you described. See here for more info: https://stackoverflow.com/a/2190431/866021 – Adil B Apr 16 '20 at 14:52
  • 1
    Thanks !! I have one question tho: "add -u will also stage deletions" --> what does that means? – Aditya Verma Apr 16 '20 at 16:47
  • 1
    This just means that running the `git add -u` command will also stage any files git was tracking that you've since deleted from the repo. Git will delete these files from the repo when you make the commit. – Adil B Apr 16 '20 at 16:51
  • 1
    With reference to my example can you tell me what will git status look like after git add -u . Let say along with the above files I have a deleted tracked file K. – Aditya Verma Apr 16 '20 at 16:57
  • 1
    It will look like this: `Modified: file x Modified: file y Modified: file z Deleted: file K` – Adil B Apr 16 '20 at 17:01
  • Thanks, just one more thing? Can you give me a use case when to use git add -u . over git add . and a use case for git add. over the git add -u . – Aditya Verma Apr 16 '20 at 17:55
  • 1
    Sure - use `git add .` when you want Git to track all of the files in your current directory (even if they were not tracked before), and use `git add -u` when you want Git to stage all your changes to the files it's tracking across your entire repo (not just the current directory). – Adil B Apr 16 '20 at 18:00

2 Answers2

4

Yes - use the git add -u command when you want Git to stage all your modifications to the files it's currently tracking across your entire repository.

Adil B
  • 14,635
  • 11
  • 60
  • 78
1

There are a lot many files and I don't want to individually add them all.

You can use git ignore and git will not add those files

### .gitignroe
<b full path>

Important: don't forget to add and commit your .gitignore file.


How to get a list of ignored files?

git check-ignore -v <path>
CodeWizard
  • 128,036
  • 21
  • 144
  • 167