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

785,939

Solution 1

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 Pushing and pulling (the article I referred to first).

Solution 2

commit: adding changes to the local repository

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

Solution 3

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

Solution 4

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

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 repository.

Share:
785,939
ben
Author by

ben

Updated on July 10, 2022

Comments

  • ben
    ben almost 2 years

    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?

  • tanascius
    tanascius about 14 years
    Here is the original source: osteele.com/archives/2008/05/my-git-workflow with another picture of a git workflow
  • ben
    ben about 14 years
    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?
  • tanascius
    tanascius about 14 years
    @ben: Yes, when github is your remote repository, a push will sent to github - see here for making github the remote repository: github.com/guides/…
  • Santa
    Santa about 14 years
    @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.
  • Abdullah
    Abdullah almost 11 years
    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.
  • Admin
    Admin over 10 years
    Your answer is basically identical to this answer, it doesn't add anything new.
  • Kokodoko
    Kokodoko about 10 years
    so... do you need to push first or commit first?
  • tanascius
    tanascius about 10 years
    @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
  • Celik
    Celik over 9 years
    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?
  • Admin
    Admin over 9 years
    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.
  • Vlas Bashynskyi
    Vlas Bashynskyi over 9 years
    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?
  • tanascius
    tanascius over 9 years
    @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
    tanascius over 9 years
    @VLAS Have a look at stackoverflow.com/questions/3082445/where-does-git-store-fil‌​es ... basically they are stored in a hidden subfolder called .git
  • Celik
    Celik over 9 years
    @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.
  • Qwerty
    Qwerty over 9 years
    So, do I Commit, Pull, Push, Sync, or Pull, Sync, Commit, Push?
  • hellodear
    hellodear almost 9 years
    @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.
  • Dr.jacky
    Dr.jacky almost 8 years
    @tanascius Is that possible to remove local repository from scenario, and commit changes to remote repository directly?
  • tanascius
    tanascius almost 8 years
    @Mr.Hyde no that is not possible. Git as a distributed version control requires you to have a local copy.
  • Dr.jacky
    Dr.jacky almost 8 years
    @tanascius How about doing something in this question: stackoverflow.com/questions/38291364/…
  • yathirigan
    yathirigan over 7 years
    Does push mean a new version release of that library ?
  • Mehrdad Salimi
    Mehrdad Salimi almost 7 years
    @GreenMatt the link is not available anymore. may you replace it. thanks
  • GreenMatt
    GreenMatt almost 7 years
    @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?
  • Mehrdad Salimi
    Mehrdad Salimi almost 7 years
    @GreenMatt ops. I am really sorry. I should have directed the post owner (tanascius)
  • tanascius
    tanascius almost 7 years
    @GreenMatt: I corrected the link - it is working again, now
  • Gobliins
    Gobliins almost 7 years
    Does git pushautomatically push code into my currently feature branch or do i have to manually enter git push origin branch ?
  • Kaschwenk
    Kaschwenk about 6 years
    Original source link from @tanascius is broken, here is the current one: blog.osteele.com/2008/05/my-git-workflow
  • Nesha Zoric
    Nesha Zoric almost 6 years
    Thank you for the great answer. It helped me out to get a better understanding. This article also helped me to get a better understanding of how other git commands work as well.
  • Legends
    Legends almost 6 years
    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..?!
  • equivalent8
    equivalent8 over 5 years
    to fully understand git watch this advanced talk youtube.com/watch?v=1ffBJ4sVUb4
  • multigoodverse
    multigoodverse over 5 years
    Does git push uploads the actual updated files or some special "diff" file?
  • Sumax
    Sumax almost 5 years
    @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.
  • Erik Thysell
    Erik Thysell almost 4 years
    @tanascius Thankyou so much, been looking for so long for a simple picture like this!
  • Lathryx
    Lathryx over 3 years
    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!
  • pholpar
    pholpar almost 3 years
    The original version as referred by @tanascius is not available any more. See the archived version here: web.archive.org/web/20080807222046/http://osteele.com/archiv‌​es/…
  • Dave F
    Dave F over 2 years
    Great concise answer! Many answers on here are unnecessarily long.
  • Peter Mortensen
    Peter Mortensen almost 2 years
    What does "Snapshot | Changeset | Version | History-record" mean? E.g., does it refer to a particular application or web site?
  • Peter Mortensen
    Peter Mortensen almost 2 years
    What do you mean by "Checkit (.git) file get created" (seems incomprehensible)? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).
  • Peter Mortensen
    Peter Mortensen almost 2 years
    OK, the OP has left the building: "Last seen more than 3 years go"
  • xged
    xged almost 2 years
    @PeterMortensen No. These are just different possible general descriptors.