An open-source distributed version management system is called Git. On top of Git, GitLab was developed. In GitLab, you may easily do various Git activities and the git command line is necessary for more difficult activities, such as fixing tricky merge conflicts or rolling back commits. This article is for beginners and new to Git, you will learn some basic git commands that you may use to boost your work efficiency, tracking codes, team collaboration and extending this feature using Gitlab.
You might need to input your credentials when starting git from your computer to prove that you are the author of the work. You should use the same username and email address as you use in GitLab.
Add your user name and email address
git config --global user.name "your_username" git config --global user.email "email@example.com"
Check your current configuration:
git config --global –-local
Getting Clone URL Repository
You may select any project you have and must have permission to access on GitLab.com
1. Go to the GitLab Repository and click on Clone
2. Copy the HTTPS/SSH Clone URL by clicking on the copy button on the right of the URL
Example Repository Clone URL:
Clone with SSH
Clone with HTTPS
git clone <repo> <directory>
When you clone a repository, your computer downloads the files from the remote repository and establishes a connection.
This connection requires you to add credentials. You can either use SSH or HTTPS. SSH is recommended.
Lets assume that we have already configured Gitlab SSH Authentication, if you haven’t done this before you may follow the official guides on Gitlab SSH Documentation
Clone with SSH
git clone firstname.lastname@example.org:rockitpinoy/webcodes.git
Or this way around
Clone with HTTPS
git clone https://gitlab.com/rockitpinoy/webcodes.git
Go to the newly created directory webcodes:
Note: A separate window will appear on screen requesting for your Gitlab Credentials, just provide them to authenticate then your are good to go.
You can initialize a local folder so Git tracks it as a repository.
A .git folder is created in your directory, this folder contains Git records and configuration files. You should not edit these files directly.
You add a “remote” to tell Git which remote repository in GitLab is tied to the specific local folder on your computer. The remote tells Git where to push or pull from.
git remote add origin email@example.com:rockitpinoy/webcodes.git
To view your remote repositories, type:
git remote -v
The -v flag stands for verbose.
Download Updated Data
When you clone a repository, REMOTE is typically origin. The repository on the remote server provides the location from which it was cloned, indicating either an SSH or HTTPS URL.
git checkout -b <branch>
Branch names cannot contain empty spaces and special characters. Use only lowercase letters, numbers, hyphens (-), and underscores (_).
To switch to an existing branch:
For example, to change to the main branch:
git checkout main
To view the differences between your local unstaged changes and the latest version that you cloned or pulled:
View Updated Works
To check which files have been changed (added, changed, or deleted files or folders)
Add And Commit Local Changes
git add git status git commit -m "YOUR COMMIT DESCRIPTION HERE"
Stage And Commit All Changes
As a shortcut, you can add all local changes to staging and commit them with one command:
git commit -a -m " YOUR COMMIT DESCRIPTION HERE"
Send Changes To Repository
To push all local changes to the remote repository:
Example: push local commits to the main branch of the origin remote:
git push origin main
Delete All Branch Changes
git checkout .
Unstage the changes that you have added to the staging area.
Undo Recent Commit
To rewind history without changing the contents of your local files
git reset HEAD~1
When you are ready to add your changes to the default branch, you merge the feature branch into it:
git checkout git merge
git fetch <remote name>
Updates your remote-tracking branches under refs/remotes//.
Example: This operation is safe to run at any time since it never changes any of your local branches under refs/heads.
git fetch origin
git pull <remote> <branch>
Brings a local branch up-to-date with its remote version, while also updating your other remote-tracking branches.
Example: Fetch changes from the branch main in the remote called origin.
git pull origin main
- Git Official Documentation – https://git-scm.com/doc
- GitLab Documentation – https://docs.gitlab.com/
- Git Basics – https://git-scm.com/book/en/v2/Git-Basics
- Getting Started – Git Basics – https://docs.gitlab.com/ee/gitlab-basics/start-using-git.html
- Create a new Git repository – https://docs.gitlab.com/ee/gitlab-basics/create-project.html
- Git Cheat Sheet – https://about.gitlab.com/images/press/git-cheat-sheet.pdf
- Git Handbook – https://guides.github.com/introduction/git-handbook/
- GitLab Flow – https://docs.gitlab.com/ee/topics/gitlab_flow.html
- Branches in Git – https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell
- Merge Requests – https://docs.gitlab.com/ee/user/project/merge_requests/index.html
- DevOpsSeptember 19, 2023Basic Git Commands For Gitlab Developers
- ContainersSeptember 18, 2023Deploy OpenVZ 7 Containers
- ContainersSeptember 10, 2023Installing Self Host NTFY On Linux Using Docker Container
- Privacy and SecurityAugust 24, 2023How To Install FREE SSL (Let’s Encrypt) With Apache2.4 On CentOS7