Git: add vs push vs commit

142,811

Solution 1

  1. git add adds your modified files to the queue to be committed later. Files are not committed

  2. git commit commits the files that have been added and creates a new revision with a log... If you do not add any files, git will not commit anything. You can combine both actions with git commit -a

  3. git push pushes your changes to the remote repository.

This figure from this git cheat sheet gives a good idea of the work flow

enter image description here

git add isn't on the figure because the suggested way to commit is the combined git commit -a, but you can mentally add a git add to the change block to understand the flow.

Lastly, the reason why push is a separate command is because of git's philosophy. git is a distributed versioning system, and your local working directory is your repository! All changes you commit are instantly reflected and recorded. push is only used to update the remote repo (which you might share with others) when you're done with whatever it is that you're working on. This is a neat way to work and save changes locally (without network overhead) and update it only when you want to, instead of at every commit. This indirectly results in easier commits/branching etc (why not, right? what does it cost you?) which leads to more save points, without messing with the repository.

Solution 2

git add selects changes

git commit records changes LOCALLY

git push shares changes

Solution 3

  • git add adds files to the Git index, which is a staging area for objects prepared to be commited.
  • git commit commits the files in the index to the repository, git commit -a is a shortcut to add all the modified tracked files to the index first.
  • git push sends all the pending changes to the remote repository to which your branch is mapped (eg. on GitHub).

In order to understand Git you would need to invest more effort than just glancing over the documentation, but it's definitely worth it. Just don't try to map Git commands directly to Subversion, as most of them don't have a direct counterpart.

Solution 4

I find this image very meaningful :

enter image description here

(from : Oliver Steele -My Git Workflow (2008) )

Solution 5

add tells git to start tracking a file.

commit commits your current changes on your local repository

push pushes you local repo upstream.

Share:
142,811
CQM
Author by

CQM

Updated on May 20, 2021

Comments

  • CQM
    CQM almost 3 years

    What is the difference between git add, push and commit?

    Just a little confused coming from SVN, where "update" will 'add' stuff, and commit does a "push" and will 'add' as well

    There are all different functions within git. Hoping for some explanation from your experience.

  • Andrew S
    Andrew S almost 9 years
    So does push commit locally and then push to remote server or does one have to commit first then push to get work to the remote repository?
  • Romain
    Romain over 8 years
    You first have to commit ("git commit") which update your local repository (a.k.a on your local machine) then you have to push to the server ("git push") which update the remote repository (on the github server for eg.)
  • Junchen Liu
    Junchen Liu over 8 years
    I would love a "mentally add" function in Git!
  • CCC
    CCC almost 8 years
    It is adviced to not only commit often, but also to push often. This protects your work when your hard drive crashes.
  • eric
    eric almost 7 years
    Now I have to go look up svn add and commit. My big fear: their docs will then refer me to git add and commit. And lord_t is the author of said docs.
  • pmaruszczyk
    pmaruszczyk about 6 years
    The question author has had knowledge of SVN, so I assumed he know svn's commands.
  • Mahdi Amrollahi
    Mahdi Amrollahi over 4 years
    Someone should explain the difference between local and remote github. What you have in local and remte. This is the key that you need to find