/// article

Basic Git Commands For Gitlab Developers

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, […]

By rockitpinoy

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. Git Configuration 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 "your_email_address@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 https://gitlab.com/rockitpinnoy/webcodes Clone with HTTPS git@gitlab.com:rockitpinoy/webcodes.git Cloning Repository 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 git@gitlab.com: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: cd 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. Initialize Project You can initialize a local folder so Git tracks it as a repository. git init A .git folder is created in your directory, this folder contains Git records and configuration files. You should not edit these files directly. Add Remote 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 git@gitlab.com:rockitpinoy/webcodes.git View Remote To view your remote repositories, type: git remote -v The -v flag stands for verbose. Download Updated Data git pull 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. Create Branch git checkout -b <branch> Branch names cannot contain empty spaces and special characters. Use only lowercase letters, numbers, hyphens (-), and underscores (_). Switch Branch To switch to an existing branch: git checkout For example, to change to the main branch: git checkout main View Differences To view the differences between your local unstaged changes and the latest version that you cloned or pulled: git diff View Updated Works To check which files have been changed (added, changed, or deleted files or folders) git status 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: git push 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. git reset Undo Recent Commit To rewind history without changing the contents of your local files git reset HEAD~1 Merge Branch When you are ready to add your changes to the default branch, you merge the feature branch into it: git checkout git merge Fetch Repository 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 Pull Repository 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 References: 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