973

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?

Asaprab
  • 288
  • 2
  • 15
ben
  • 28,169
  • 41
  • 123
  • 175
  • 92
    You can also check out the "Git cheatsheet" that helped me a lot to understand the concept : http://ndpsoftware.com/git-cheatsheet.html – adriendenat Mar 31 '13 at 18:00
  • 1
    Here's another cheat sheet: https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet – Kellen Stuart Aug 13 '18 at 14:49
  • 5
    no answer since it has already been provided. just an observation. seems to me a commit should really be called a save and the push should be called commit. Classic naming scenario issue. – dublinx Sep 10 '19 at 00:42

16 Answers16

1744

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)

tanascius
  • 51,705
  • 21
  • 111
  • 133
  • 26
    Here is the original source: http://osteele.com/archives/2008/05/my-git-workflow with another picture of a git workflow – tanascius Apr 30 '10 at 14:28
  • Ah that makes sense, thanks a lot! A quick follow up if I may, in the tute I'm doing, the author makes it seem like setting up GitHub is totally optional, but then starts using git push. Does using push send to GitHub, if that's what you've setup? – ben Apr 30 '10 at 14:50
  • @ben: Yes, when github is your remote repository, a `push` will sent to github - see here for making github the remote repository: http://github.com/guides/setting-up-a-remote-repository-using-github-and-osx – tanascius Apr 30 '10 at 15:15
  • 10
    @ben github is but one solution to host your public, "on-the-cloud", repository for `git push` to work with. In reality, the destination of `git push` can be *any* git repository. It can be on your own local hard drive in another directory (`git remote add clone ~/proj/clone.git; git push clone master` or `git push ~/proj/clone.git master`, for example), or a git repository that your *own* host serves. – Santa May 02 '10 at 18:22
  • 3
    so... do you need to push first or commit first? – Kokodoko Apr 01 '14 at 20:24
  • 5
    @Piet it starts at your workspace, where you modify files. Then you add them to the index, commit them to the local repository and - finally - push them to the remote repository – tanascius Apr 03 '14 at 08:11
  • 1
    Why push and commit are separated? When I commit something, I always push it at the same time. So, why I need both of them separately? note: I know commit works locally, push works for remotely. Any other reason or usage? – Celik Aug 12 '14 at 13:29
  • So when i edit a file in a text editor i don't change files in my local repository, right? Where are my local repository files stored then? – Vlas Bashynskyi Aug 22 '14 at 06:59
  • 1
    @Celik When you have no remote repository, a push is useless. So the separation makes sense. But there might be more then one remote repostitory as well - so you have to push multiple times. – tanascius Aug 22 '14 at 13:25
  • @VLAS Have a look at http://stackoverflow.com/questions/3082445/where-does-git-store-files ... basically they are stored in a hidden subfolder called `.git` – tanascius Aug 22 '14 at 13:26
  • 1
    @tanascius one command is still enough. If there is a remote repo, this command pushes it. if there is no remote commit it. Still second one is useless. for the multiple repo,one command is still enough. e.g: **git push repo1 repo2 ...**. I can push them all with one command. – Celik Aug 22 '14 at 13:33
  • So, do I `Commit, Pull, Push, Sync`, or `Pull, Sync, Commit, Push`? – Qwerty Oct 24 '14 at 14:26
  • @tanascius when I change file locally that is my local repository, right? when I go in .git, do u mean in .git we have some other copy of the files which I am editing? when I say commit, then these files .git are over-written by the workspace file? Please tell. – hellodear May 28 '15 at 12:38
  • @tanascius Is that possible to remove local repository from scenario, and commit changes to remote repository directly? – Dr.jacky Jul 13 '16 at 09:33
  • 2
    @Mr.Hyde no that is not possible. Git as a distributed version control requires you to have a local copy. – tanascius Jul 15 '16 at 06:55
  • @tanascius How about doing something in this question: http://stackoverflow.com/questions/38291364/controlling-git-repository-remotely – Dr.jacky Jul 16 '16 at 03:34
  • Does push mean a new version release of that library ? – yathirigan Dec 21 '16 at 06:43
  • @GreenMatt the link is not available anymore. may you replace it. thanks – Mehrdad Salimi May 30 '17 at 22:17
  • @MehrdadSComputer: Huh? Not sure which link you mean, but my only comment on here contains no link. Did you mean to direct that to someone else? – GreenMatt May 31 '17 at 14:36
  • @GreenMatt ops. I am really sorry. I should have directed the post owner (tanascius) – Mehrdad Salimi May 31 '17 at 15:06
  • @GreenMatt: I corrected the link - it is working again, now – tanascius Jun 01 '17 at 12:51
  • 1
    Does `git push`automatically push code into my currently feature branch or do i have to manually enter `git push origin branch` ? – Gobliins Jul 26 '17 at 14:35
  • 1
    Original source link from @tanascius is broken, here is the current one: https://blog.osteele.com/2008/05/my-git-workflow/ – Kaschwenk Apr 06 '18 at 10:55
  • Thank you for the great answer. It helped me out to get a better understanding. [This article](https://kolosek.com/git-commands-tutorial-part1/) also helped me to get a better understanding of how other git commands work as well. – Nesha Zoric May 07 '18 at 12:16
  • What is a workspace and what is a local repository ??? I thought this is the same and my repo on github is my remote repo..?! – Legends May 16 '18 at 18:45
  • to fully understand git watch this advanced talk https://www.youtube.com/watch?v=1ffBJ4sVUb4 – equivalent8 Aug 15 '18 at 17:40
  • @Gobliins: hope it's not too late; yes you are right you must specify `git push origin branch`, for eg: 'branch' = 'master' if you are updating the main repo. – Sumax Jul 25 '19 at 17:12
  • @tanascius Thankyou so much, been looking for so long for a simple picture like this! – Erik Thysell May 07 '20 at 11:53
  • From what I understand, "commit" is essentially "staging" the changes, whilst "push" is actually making those changes. If anything it sounds better the other way around in my opinion. Thanks, though! – Lathryx Nov 02 '20 at 20:58
  • The original version as referred by @tanascius is not available any more. See the archived version here: http://web.archive.org/web/20080807222046/http://osteele.com/archives/2008/05/my-git-workflow – pholpar Jul 04 '21 at 08:54
250

commit: adding changes to the local repository

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

nbro
  • 13,796
  • 25
  • 99
  • 185
TheHippo
  • 58,689
  • 15
  • 73
  • 98
73

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

markovuksanovic
  • 15,126
  • 11
  • 43
  • 56
  • 12
    this is my second day of using GIT. As I look at the answers above, i still don't get a clear picture, but your answer just nails it. thanks. – Bopha May 13 '13 at 19:47
  • 2
    Does `git push` uploads the actual updated files or some special "diff" file? – multigoodverse Dec 06 '18 at 11:43
30

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.

Yamaneko
  • 3,255
  • 2
  • 33
  • 56
Michael Borgwardt
  • 335,521
  • 76
  • 467
  • 706
27

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.

Justin Ethier
  • 127,537
  • 50
  • 225
  • 279
27

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.

xged
  • 1,136
  • 1
  • 13
  • 20
12

git commit record your changes to the local repository.

git push update the remote repository with your local changes.

nbro
  • 13,796
  • 25
  • 99
  • 185
Naresh
  • 612
  • 4
  • 16
  • 23
    Your answer is basically identical to [this answer](http://stackoverflow.com/questions/2745076/what-is-the-difference-between-git-commit-and-git-push/2745107#2745107), it doesn't add anything new. –  Aug 14 '13 at 05:30
11

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

DEVINDER THAKUR
  • 111
  • 1
  • 2
7

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
Faisal Shaikh
  • 3,760
  • 4
  • 37
  • 70
5

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.

amn
  • 7,742
  • 8
  • 54
  • 82
1

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.

Cibin Joseph
  • 1,034
  • 1
  • 11
  • 16
0

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)

Sai Koti
  • 147
  • 1
  • 1
  • 10
0

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

-3

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.

Marcus Thornton
  • 5,503
  • 5
  • 45
  • 47
  • Some people may want to force push instead of pull. It depends on the situation. In fact, if you're rebased commits on a branch that you're not sharing with other people (even on a remote repo), then pulling is certainly *not* what you want to do. –  Aug 12 '14 at 14:03
-4

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

oroyo segun
  • 325
  • 2
  • 4
-4

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

Zeal Murapa
  • 730
  • 7
  • 16