How to git push GitLab commits to origin by example
In previous tutorials we explored how to perform a GitLab clone and pull files from a GitLab project on the server to a Git repository on the local machine.
In this tutorial we will look at how to perform the opposite set of operations. That is, we will demonstrate how to add files to a local Git repository, perform a commit and then push GitLab commits to origin. The origin refers to the colloquial name we give to the server from which we originally performed the clone. Let’s dive into how to ‘git push’ GitLab commits to the origin.
GitLab repo enhancement
The first step is to add a new file to the user’s local repository. A developer can achieve this in a variety of ways, be it through an IDE, text editor or any software program of preference. However, I will take the simplest approach possible for this example and create the file with the ‘touch’ command.
With the file created, the next steps are to add the file to the Git index and commit the changes against the local repository.
Use the ‘git push origin’ command to move the local commit to the remote GitLab repository.
It’s also a good idea to review the process ensure no files failed to be added to the commit. Developers can run ‘git status’ and ‘git reflog’ commands after they have performed the GitLab push of commits to origin.
Before you ‘git push’ GitLab commits to origin, doublecheck that you didn’t forget to add files to the commit by issuing the ‘git status’ command.
‘Git status’ check
The ‘git status’ command informs developers of any untracked files, modified files and new files that haven’t been added to the index and haven’t been included in any commits. If such a situation arises, re-run the ‘git add’ command and create a new commit. Then you will be ready to perform a ‘git push’ of GitLab commits.
gitlab@example:~/gitlab-made-easy$ git status On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
Steps to ‘git push’ GitLab commits to origin
What’s involved in a push? Not much. Just follow this list of steps to ‘git push’ GitLab commits to origin:
- Add your files to the Git index;
- Perform a commit with a well written Git commit message;
- Issue a ‘git push’ command, optionally specifying the branch of interest; and
- Run a ‘git log’ or ‘git reflog’ command to view the git commit history.
The GitLab push command
The sequence of steps performed at the BASH terminal to push GitLab commits to origin are:
gitlab@example:~/gitlab-made-easy$ touch home.html gitlab@example:~/gitlab-made-easy$ touch script.js gitlab@example:~/gitlab-made-easy$ touch style.css gitlab@example:~/gitlab-made-easy$ git add . gitlab@example:~/gitlab-made-easy$ git commit -m "GitLab push commits to origin" gitlab@example:~/gitlab-made-easy$ git status gitlab@example:~/gitlab-made-easy$ git push origin gitlab@example:~/gitlab-made-easy$ git reflog
Once the commands to push GitLab commits to origin have completed, log into the GitLab administrative console and examine the master branch. You will see a new commit, and will notice that three new files have been added to the master branch. This proves that the ‘git push’ of GitLab commits to origin has completed successfully.
GitLab push authentication
Be aware that the user who performs the ‘git push‘ must have access rights to the GitLab project. On a public repository, any user can perform a GitLab git clone. However, a GitLab push to origin requires user authentication, and if there is a GitLab branch permission, the user might also have to be in the developer or maintainer role. If you have a ‘git push’ error, make sure there isn’t an authentication or rights issue.
While this example used an on-premises GitLab Ubuntu installation, the steps apply equally to Windows, Linux and the cloud-hosted GitLab offering.
The source code and sample project used for these examples can be found on the gitlab-made-easy project page on GitLab.
Check out this how to use GitLab tutorial to learn more.