Git: add vs push vs commit

GitRepositoryPushCommitAdd

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

  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

git add selects changes

git commit records changes LOCALLY

git push shares changes

Solution 3 - Git

  • 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 - Git

I find this image very meaningful :

enter image description here

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

Solution 5 - Git

I was confused about what 'add' really does. I just read a very enlightening paragraph from the book Git Pro that I'd like to add here, because it clarifies things

> It turns out that Git stages a file exactly as it is when you run the > git add command. If you commit now, the version of benchmarks.rb as it > was when you last ran the git add command is how it will go into the > commit, not the version of the file as it looks in your working > directory when you run git commit. If you modify a file after you run > git add, you have to run git add again to stage the latest version of > the file

Excerpt From: Chacon, Scott. “Pro Git.” Springer, 2009-08-19T00:00:00+00:00. iBooks. This material may be protected by copyright.

Solution 6 - Git

add tells git to start tracking a file.

commit commits your current changes on your local repository

push pushes you local repo upstream.

Solution 7 - Git

Very nice pdf about many GIT secrets.

Add is same as svn's add (how ever sometimes it is used to mark file resolved).

Commit also is same as svn's , but it commit change into your local repository.

Solution 8 - Git

add -in git is used to tell git which files we want to commit, it puts files to the staging area

commit- in git is used to save files on to local machine so that if we make any changes or even delete the files we can still recover our committed files

push - if we commit our files on the local machine they are still prone to be lost if our local machine gets lost, gets damaged, etc, to keep our files safe or to share our files usually we want to keep our files on a remote repository like Github. To save on remote repositories we use push

example Staging a file named index.html git add index.html

Committing a file that is staged git commit -m 'name of your commit'

Pushing a file to Github git push origin master

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCQMView Question on Stackoverflow
Solution 1 - GitabcdView Answer on Stackoverflow
Solution 2 - GitDustinView Answer on Stackoverflow
Solution 3 - GitAdam ByrtekView Answer on Stackoverflow
Solution 4 - GitMalickView Answer on Stackoverflow
Solution 5 - Gituser2489252View Answer on Stackoverflow
Solution 6 - GithvgotcodesView Answer on Stackoverflow
Solution 7 - GitpmaruszczykView Answer on Stackoverflow
Solution 8 - GitHerbertView Answer on Stackoverflow