Version Control Basics

Version Control Basics

Git is a version control system that allows its users to see code changes made over time and thus creates a way for multiple users to collaborate on code whereas GitHub is cloud-based coding platform for git.

To get started with git, it is necessary to install it on your Linux machine which can be done via the Linux command line terminal. The command below installs git.

sudo apt install git -y


We'll also need to configure git by adding our credentials which can be done with
git config --global user.name "YOUR-NAME"
git config --global user.email "YOUR-EMAIL"

To verify that you have properly entered the latter, the following command can be used:

git config --global --list


Some fundamentals of Git are:
Creating a repository - Commit - Cloning a repository

Creating a repository

We'll need to create a folder to house that repository, which can be done using the mkdir command:

mkdir ~/ A-LOCAL-REPOSITORY-NAME

We are now going to initiate that repository by changing our present directory to this folder and run the command:

git init


If you have not configured git, there will be hint messages as shown above.

We'll create a README text file which will contain the information that you want to give to other developers using the command:

nano README.txt

The nano editor makes it easy to write files on the go while offering plenty of functionality.
[Ctrl] + [G] brings up the help menu in case you are not familiar with nano.
It should look like this when you are done with your README.txt file.



This is just a regular file, to make it work and recognized by git, we'll execute a second command:

git add README.txt
or
git add .
(if you have more files)


"git status" is a useful command for checking the status of your repository



There is now a change to be committed, so the next step is to create a commit for our newly added README.txt file.

Commit

When you create a commit, you let others know the changes that have been made for this file. We just added the README.txt file, so we'll want to create a commit to indicate that very thing using this command:

git commit -m "Added README.txt"


Along with README txt, I have also committed my assignment on it.
now if you type in the git status command. This should appear

We are going to rename the branch as "main" using:

git branch -M main


- Pushing commits

A GitHub account is required to proceed.

You will also need to have created an access token, which is done in GitHub Settings > Developer Settings > Personal Access Tokens. Once you've created a Personal Access Token, make sure to copy it, as you cannot view it again without regenerating it.


To effectuate these changes we'll use the command:

git remote add origin Link-to-your-github's-git
git push -u origin Your-branch-name (main in this case)

Note that this will ask you for your GitHub username and password in the form of the token you have generated earlier. Successful Authentication should look like this:


The pushed commit will appear as repository on GitHub

You'll see that a new repository has been created that includes all of the files in your local repository when you log in to your GitHub account.

At this point you can add new files or edit existing files from GitHub in your web browser. If you do that, you can then pull down those changes to your local repository, see this command for more information:

git pull --help

Any file that you added to the GitHub repository will be pulled down and available to your local repository for editing

Cloning a repository
Cloning a repository makes it easier to fix merge conflicts, add or remove files, and push larger commits.

Cloning is basically copying the repository from GitHub.com to your local machine, or to a remote virtual machine when you create a codespace.

Firstly, we find the repository you want to clone, I will be cloning my own repository.

There are 3 ways of cloning a repository


1. Using HTTPS
2. Using SSH key
3. Using GitHub CLI

We are going to use HTTPS, so copy the URL

Now open Git Bash(Your terminal with git functionality), change the current working directory to where you want to have your cloned directory. Use the following command:

git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY-NAME


If this is what you see, then you have successfully cloned an online repository to your local machine.

These were the basics of version control and the gist of the git workflow.

GitHub: https://github.com/DerekLC08


Comments

Popular posts from this blog

Simple Linux Commands

Installing Ubuntu on Windows 11 with python support on VirtualBox