Skip to content

Git - Push Operation

After committing changes to your local repository, the git push command is used to upload these local commits to a remote repository. This makes your changes accessible to other collaborators and serves as a backup. For example, Jerry has made some commits and perhaps amended his last commit using git commit --amend.

He first executes git log to view the recent commit details on his local main branch:

[jerry@devbox project]$ git log -1

The above command will produce output similar to:

commit d1e19d316224cddc437e3ed34ec3c931ad803958 (HEAD -> main)
Author: Jerry Mouse <jerry@example.com>
Date: Tue Jan 16 09:15:00 2024 +0000
Refactor my_strlen to use size_t and const pointer

Before pushing, it’s good practice to review the changes that will be pushed. He can use git show with the commit ID (or HEAD for the latest commit) to see the commit details and the actual changes (diff):

[jerry@devbox project]$ git show HEAD

The above command will produce output showing the commit info and the diff:

commit d1e19d316224cddc437e3ed34ec3c931ad803958 (HEAD -> main)
Author: Jerry Mouse <jerry@example.com>
Date: Tue Jan 16 09:15:00 2024 +0000
Refactor my_strlen to use size_t and const pointer
diff --git a/string_utils.c b/string_utils.c
index 7da2992..0000000 100644
--- a/string_utils.c
+++ b/string_utils.c
@@ -1,15 +1,15 @@
#include <stdio.h>
-int my_strlen(char *s) // Old version
+size_t my_strlen(const char *s) // New version: returns size_t, takes const char*
{
- char *p = s;
+ const char *p = s;
while (*p)
++p;
return (p - s);
}
int main(void)
{
int i;
char *s[] =
{
"Git tutorials",
"Tutorials Point"
};
for (i = 0; i < 2; ++i)
- printf("string length of %s = %d\n", s[i], my_strlen(s[i])); // Old printf
+ printf("string length of \"%s\" = %zu\n", s[i], my_strlen(s[i])); // New printf with %zu
return 0;
}

Jerry is satisfied with his changes and is ready to push them to the main branch on the origin remote repository.

[jerry@devbox project]$ git push origin main

The above command will produce output similar to:

Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to X threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 517 bytes | 517.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
To https://git.example.com/our_team/project.git
abc1234..d1e19d3 main -> main

Jerry’s changes have been successfully pushed to the remote repository. Now, other developers can fetch or pull these changes to update their local copies. If the remote branch had changes Jerry didn’t have, the push might be rejected (requiring a git pull first), as seen in the ‘Git - Update Operation’ chapter.