0

Is it necessary to run git add . command because running git commit -m "push" and then git push command can also push our code to the remote repository without running git add . command?

Wasiq Ali
  • 26
  • 5
  • 1
    git add is only relevant in terms of what you will _commit_ into the revision you will create. When pushing, it has _no relevance_ because what will be pushed is what **is** the revision/revisions of a branch. Now, you can skip using `git add` if you mean to commit everything in a given moment (like `git commit -a -m "committing everything"`) but that's not the way you will want to work in the long run. I could decide to commit only some of the files that I have modified at a given moment in time.... or even **a section** of a modified file... and then you will want to use `git add`). – eftshift0 Aug 10 '22 at 11:48
  • Also `git add .` only adds files from the current folder, so you will not always have this special case of adding. – YesThatIsMyName Aug 10 '22 at 11:49
  • Again, you are free to skip using the staging area.... I am not saying otherwise.... but in the long run, you will want to take advantage of it. – eftshift0 Aug 10 '22 at 11:50

2 Answers2

1

To answer your question: yes, it is necessary to stage changes/files before you (1) commit and then (2) push them, otherwise they will not be pushed to the remote repository.

It is wrong that running git commit -m "push" and then git push command can also push your code to the remote repository without running git add ..

git add expects a list of files as a parameter. The . in git add . just says stage all current changes from the current folder.

YesThatIsMyName
  • 1,585
  • 3
  • 23
  • 30
0

git add . - stages all edits from the current directory (that's what . stands for)
git commit -m "push' - commits all files you've staged with the message (that's what (-m) stands for) "push"
git push - pushes the commits you've added to the remote repository

  • It is necessary to specify what files you want to add before committing, otherwise git would not know what modifications you want to save.
  • If you want to add all files and commit using a single command use git commit -am (note that this shorter command will not stage untracked files (a new class for example)))

EDIT: Make sure to check @eftshift0 's comments for further explanation

Vlad Nitu
  • 147
  • 1
  • 11