Git Commit vs Git Push
In this tutorial, we will learn the difference between git commit
and git push
.
Git is a distributed version control system that tracks the changes of the files, usually in a collaborative development environment.
Git gives each developer (i.e.) each machine a local copy of the full history of the project directory that is tracked as a repository. Any local changes are then copied from the local repository to the remote repository.
Also, any changes on the remote repository are fetched into the local repository.
Git provides commands git commit
and git push
to achieve these goals.
We will now elaborate on the difference between them with an example.
Difference Between git commit
and git push
in Git
The basic difference between git commit
and git push
is that the scope of the git commit
is the local repository, and that of git push
is the remote repository.
The git push
command always comes after executing the git commit
command.
When we execute a git commit
command, a snapshot of the project’s currently staged changes is captured. The git add
command does the staging of the changes.
The git push
command pushes the local repository content to a remote repository. This command transfers the commits from the local repository to the remote repository.
Let’s say we have a file named sample.txt
in our local repository, which we have updated, and also have staged the changes of the file with the git add
command.
Now, we will check the status of the local repository as follows.
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: sample.txt
We can see that the changes of the file sample.txt
are shown as ready to be committed.
We will now use the git commit
command to do this. The syntax of the git commit
command to commit changes with a message is, git commit -m <message>
.
Thus, we will do as follows.
$ git commit -m "updated sample.txt"
We will now check the Git log of the local repository as follows.
$ git log --oneline
4488776 (HEAD -> main) updated sample.txt
...
We now can see in the Git log, the commit for the file sample.txt
is shown. This commit is at the HEAD
of the local repository index. The new commit is the direct child of the HEAD
of the index, and the branch viz. main
is updated to point to it.
We will now execute the git push
command to push the commit to the remote repository. The syntax of the git push
command is git push <remote-repository> <branch>
.
Thus, we will do as follows.
$ git push origin main
We have now pushed the commit to the remote repository, given by the alias origin
, and the remote branch main
.
We will now recheck the Git log as follows.
$ git log --oneline
4488776 (HEAD -> main, origin/main) updated sample.txt
...
In the Git log, we can now see that the commit for the file sample.txt
is shown.
The commit is now both at the HEAD
of the index of the local repository and in the remote branch of the remote repository.
Thus, we have elaborated the difference between the commands git commit
and git push
in Git.
Related Article - Git Commit
- How to Get Current Commit in Git
- How to List Commits in Git
- How to Have Git Add and Git Commit in One Command
- How to git add, git commit, and git push in One Command
- How to Commit Current Changes to a Different Branch in Git
- How to Exit the Commit Message Editor