Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why commit without a push in git?

I am starting to learn Git. I've been messing around with GitKraken, and I hope that using a GUI is a viable path for saving and sharing stuff. I prefer GUIs to the command line.

Now I understand, in the GUI, I need to stage files, then commit them and then push, to upload them. But why are push and commit two different things? Why would want to just commit locally, without the following upload to keep files synced?

like image 492
marko-36 Avatar asked Aug 12 '18 12:08

marko-36


People also ask

Do we need to push after commit in git?

The git push command allows you to send (or push) the commits from your local branch in your local Git repository to the remote repository. To be able to push to your remote repository, you must ensure that all your changes to the local repository are committed.

Do I need to push every commit?

Typically pushing and pulling a few times a day is sufficient. Like @earlonrails said, more frequent pushes means less likelihood of conflicting changes but typically it isn't that big a deal. Think of it this way, by committing to your local repository you are basically saying "I trust this code. It is complete.

Is it necessary to commit before push?

Yes, you do need add and commit your changes before pushing your code to your github repository.

What is the difference between commit & push?

Commit - committing is the process which records changes in the repository. Think of it as a snapshot of the current status of the project. Commits are done locally. Push - pushing sends the recent commit history from your local repository up to GitHub.


1 Answers

A couple of (concrete) reasons. TLDR in bold.

The first reason is you might not be able to push to the remote, because you aren't connected to the internet.

The second reason is that sometimes commits aren't ready to be pushed. For example, I practice TDD. After I commit a failing test (which I do), I don't want to push that to the remote, because then my team may pull in a failing test suite. Another common practice is to squash these commits before pushing them out so it looks like the feature was one single commit.

Third reason is that it's a personal project that you don't want to share with anyone, and so there is no remote to push to at all. You don't have to have a remote with a git repo.

Fourth reason is you may have multiple remotes (in general, distributed semantics get in the way). So after you commit, how would git know which remote you want to push to? All of them? A certain one? A certain two out of the seven? Pushing must be explicit from git's perspective due to the semantics of git being a distributed VCS. See poke's answer for more detail.

P.S. This is off topic to the question, but the command line shells (including bash) are extremely powerful interfaces once you learn to use them. After you become proficient with these tools, using them is much faster than any GUI you're going to find. This has to do with the fact that you can do powerful actions with keystrokes instead of the mouse, and that it's infinitely customizable to fit your needs.

like image 183
Matt Messersmith Avatar answered Sep 18 '22 18:09

Matt Messersmith