Using GIT in Linux

2 minutes read

Linux_logo_errorbits.com

How to use GIT in Linux

GIT is a revision control system that allows multiple contributors to work on same projects/files. Is also useful if you have more servers/vms/workstation that you manage and you want to deploy “recipes”/scripts/configurations on them. With GIT, you work locally, push the changes to the repository server and then get your files from any location you want. GIT tracks the changes made to the files and if a config is not working it can be reverted in no time.

To start learning GIT, I recommend  using GitLab, is free, very versatile and your repository will be in private mode. You can also try GitHub, free version is in public mode (!!! Do not store passwords or sensitive information in your files, if you use repositories that are in public mode !!!).

Preparing GIT environment:

Open Linux Terminal in ROOT mode:

sudo bash

Install GIT:

yum install git (CentOS)

apt-get install git (Ubuntu)

Install xclip (useful to copy file contents to clipboard):

yum install xclip (CentOS)

apt-get install xclip (Ubuntu)

Create and activate an account on GitLab

Add GIT username:

git config –global user.name “USERNAME”

git config –global user.name (verify your username)

Add GIT e-mail address:

git config –global user.email “your_email@domain.com”
git config –global user.email (verify e-mail address)
git config –global –list (verify username and e-mail)

Check if your system has a SSH Key so you can link it with your GitLab account:

 cat ~/.ssh/id_rsa.pub (if it’s displayed something starting with ssh-rsa, you don’t have to follow the next steps about key creation)

Creating SSH key:

ssh-keygen -t rsa -C “your_email@domain.com” -b 4096

Changing SSH key (optional):

ssh-keygen -p <keyname>

Copy key to clipboard with xclip:

xclip -sel clip < ~/.ssh/id_rsa.pub

Go to your Profile settings in GitLab -> SSH Keys and paste the contents of the clipboard.

Do a checkout on your master branch:

git checkout master

Download the latest changes:

git pull REMOTE NAME-OF-BRANCH -u

Create a new branch:

git checkout -b NAME-OF-BRANCH

Work on the branch:

git checkout NAME-OF-BRANCH

View the changes that you have made:

git status

Commit your changes:

git add file_name.extension

git commit -m “short description”

Send changes to your gitlab.com:

git push REMOTE NAME-OF-BRANCH

After you have made all the above changes, you can use short commands, git pull to get the latest updates and git push to upload the current modifications.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To initialize a Git repository in a new project, follow these steps:Open your project directory in a terminal or command prompt.Initialize a new Git repository by running the command: git init.This will create a hidden .git directory, which contains all the ne...
Git hooks are scripts that can be executed automatically whenever certain actions occur in a Git repository. By using Git hooks, you can automate various tasks and enforce certain workflows in your development process.To use Git hooks for automation, follow th...
Creating and applying Git tags is a useful way to label specific points in a Git repository&#39;s history. Tags can be used to mark significant versions or milestones in a project. Here&#39;s how you can create and apply Git tags:Creating a Git tag: To create ...
To clone a subset of Git branches, you can follow these steps:Open a terminal or Git bash.Navigate to the directory where you want to clone the repository.Clone the repository using the git clone command followed by the repository URL: git clone &lt;repository...
To view the Git commit history, you can use the git log command in your terminal or command prompt. This command will display a chronological list of commits in your Git repository. Here are the steps to do it:Open your terminal or command prompt. Navigate to ...
To delete the merge history of a file in Git, you can follow these steps:Open a terminal or Git Bash and navigate to the directory of your Git repository. Use the git log --follow command to view the complete history of the file, including merge commits. Make...