What are the differences between "git commit" and "git push"?

GitPushGit CommitGit PushGit Index

Git Problem Overview


In a Git tutorial I'm going through, git commit is used to store the changes you've made.

What is git push used for then?

Git Solutions


Solution 1 - Git

Basically git commit "records changes to the repository" while git push "updates remote refs along with associated objects". So the first one is used in connection with your local repository, while the latter one is used to interact with a remote repository.

Here is a nice picture from Oliver Steele, that explains the git model and the commands:

Git data transport commands

Read more about git push and git pull on GitReady.com (the article I referred to first)

Solution 2 - Git

commit: adding changes to the local repository

push: to transfer the last commit(s) to a remote server

Solution 3 - Git

Well, basically git commit puts your changes into your local repo, while git push sends your changes to the remote location.

Solution 4 - Git

git push is used to add commits you have done on the local repository to a remote one - together with git pull, it allows people to collaborate.

Solution 5 - Git

Since git is a distributed version control system, the difference is that commit will commit changes to your local repository, whereas push will push changes up to a remote repo.

Solution 6 - Git

Commit: Snapshot | Changeset | Version | History-record | 'Save-as' of a repository. Git repository = series (tree) of commits.

Local repository: repository on your computer.

Remote repository: repository on a server (Github).

git commit: Append a new commit (last commit + staged modifications) to the local repository. (Commits are stored in /.git)

git push, git pull: Sync the local repository with its associated remote repository. push - apply changes from local into remote, pull - apply changes from remote into local.

Solution 7 - Git

git commit record your changes to the local repository.

git push update the remote repository with your local changes.

Solution 8 - Git

Three things to note:

1)Working Directory ----- folder where our codes file are present

2)Local Repository ------ This is inside our system. When we first time make COMMIT command then this Local Repository is created. in the same place where is our Working directory ,
Checkit ( .git ) file get created.
After that when ever we do commit , this will store the changes we make in the file of Working Directory to local Repository (.git)

3)Remote Repository ----- This is situated outside our system like on servers located any where in the world . like github. When we make PUSH command then codes from our local repository get stored to this Remote Repository

Solution 9 - Git

Just want to add the following points:

Yon can not push until you commit as we use git push to push commits made on your local branch to a remote repository.

The git push command takes two arguments:

A remote name, for example, origin A branch name, for example, master

For example:

git push  <REMOTENAME> <BRANCHNAME> 
git push  origin       master

Solution 10 - Git

A very crude analogy: if we compare git commit to saving an edited file, then git push would be copying that file to another location.

Please don't take this analogy out of this context -- committing and pushing are not quite like saving an edited file and copying it. That said, it should hold for comparisons sake.

Solution 11 - Git

It is easier to understand the use of the git commands add and commit if you imagine a log file being maintained in your repository on Github. A typical project's log file for me may look like:

---------------- Day 1 --------------------
Message: Completed Task A
Index of files changed: File1, File2

Message: Completed Task B
Index of files changed: File2, File3
-------------------------------------------

---------------- Day 2 --------------------
Message: Corrected typos
Index of files changed: File3, File1
-------------------------------------------
...
...
...and so on

I usually start my day with a git pull request and end it with a git push request. So everything inside a day's record corresponds to what occurs between them. During each day, there are one or more logical tasks that I complete which require changing a few files. The files edited during that task are listed in an index.

Each of these sub tasks(Task A and Task B here) are individual commits. The git add command adds files to the 'Index of Files Changed' list. This process is also called staging and in reality records changed files and the changes performed. The git commit command records/finalizes the changes and the corresponding index list along with a custom message which may be used for later reference.

Remember that you're still only changing the local copy of your repository and not the one on Github. After this, only when you do a git push do all these recorded changes, along with your index files for each commit, get logged on the main repository(on Github).

As an example, to obtain the second entry in that imaginary log file, I would have done:

git pull
# Make changes to File3 and File4
git add File3 File4
# Verify changes, run tests etc..
git commit -m 'Corrected typos'
git push

In a nutshell, git add and git commit lets you break down a change to the main repository into systematic logical sub-changes. As other answers and comments have pointed out, there are ofcourse many more uses to them. However, this is one of the most common usages and a driving principle behind Git being a multi-stage revision control system unlike other popular ones like Svn.

Solution 12 - Git

git commit is nothing but saving our changes officially, for every commit we give commit message, once we are done with commits we can push it to remote to see our change globally

which means we can do numerous commits before we push to remote (we can see the list of commits happened and the messages too) git saves each commit with commit id which is a 40 digit code

and I use git push only when i wanted to see my change in remote (There after i will check whether my code worked in jenkins)

Solution 13 - Git

When you commit your changes, you save the changes as a single logical set in your local repository. You can do this multiple times without pushing. Until they are pushed, they do not leave your local repository meaning the remote repository won't have these sets of changes yet, so when other people pull from the remote repository, your commits won't be pulled.

When you push, all the commits you made in your local repository will be transferred to the remote repository, so when other developers who share this remote repository pull, they will have your changes transferred to their local repositories check Git Commands and Cheat Sheet here

Solution 14 - Git

git commit is to commit the files that is staged in the local repo. git push is to fast-forward merge the master branch of local side with the remote master branch. But the merge won't always success. If rejection appears, you have to pull so that you can make a successful git push.

Solution 15 - Git

Well, basically git commit puts your changes into your local repo, while git push sends your changes to the remote location. Since git is a distributed version control system, the difference is that commit will commit changes to your local repository, whereas push will push changes up to a remote repo

source Google

http://gitref.org/basic/ this link will be very useful too

https://git-scm.com/docs/git-commit

Solution 16 - Git

in layman terms, git commit is the step before git push you run them in that order to successfully git your file to github.

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
QuestionbenView Question on Stackoverflow
Solution 1 - GittanasciusView Answer on Stackoverflow
Solution 2 - GitTheHippoView Answer on Stackoverflow
Solution 3 - GitmarkovuksanovicView Answer on Stackoverflow
Solution 4 - GitMichael BorgwardtView Answer on Stackoverflow
Solution 5 - GitJustin EthierView Answer on Stackoverflow
Solution 6 - GitxgedView Answer on Stackoverflow
Solution 7 - GitNareshView Answer on Stackoverflow
Solution 8 - GitDEVINDER THAKURView Answer on Stackoverflow
Solution 9 - GitFaisal ShaikhView Answer on Stackoverflow
Solution 10 - GitamnView Answer on Stackoverflow
Solution 11 - GitCibin JosephView Answer on Stackoverflow
Solution 12 - GitSai KotiView Answer on Stackoverflow
Solution 13 - GitlokeshjoshiView Answer on Stackoverflow
Solution 14 - GitMarcus ThorntonView Answer on Stackoverflow
Solution 15 - Gitoroyo segunView Answer on Stackoverflow
Solution 16 - GitZeal MurapaView Answer on Stackoverflow