How to use Git VCS: The Basics


Knowing Git and GitHub is essential for every developer. While GitHub is a home for a project, Git is the tool to send it there from our local disk and to manupulate it.

As we read in Git documentation: Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. What it means is that we can save our project - commit changes - at any time. The are numerous advantages of doing it, but the most significant ones include:

1. We can go back to the version of our project from any of the commits we made.

2. Our code is safely stored away and, should anything happen to our local device, we are not going to lose our code.

...and the most important one...

3. We can easily share our code and collaborate on projects with other developers.

Below you can find the most common commands that you will need to be able to use Git Version Control System.

Git Init

First we need to initialize a new repository. In order to do that we need to open Git Bash (or any other CLI, assuming we've previously installed Git on our device), navigate to a directory we want to work in or create a new directory like so:

mkdir directory_name & cd directory_name
Now, that we are inside of our directory, we can create some files that will be commited to our GitHub repository.
touch index.html index.css
Next step is to initialize a repository with the command:
git init

Git Clone

If you want to download an existing repository to your local device you need to use the command below, which will copy the specified repository to the directory you're currently in.

git clone https://repository_link.git
Now you can navigate to the downloaded project root folder to start making any changes.

Git Add

Now that we have created some files - made changes to our project - we can commit them to our new repository. Type:

git add index.css
to commit only index.css file OR...
git add .
to commit all the changes that we've made to our project since the last commit.

Git Commit

After we've added the changes, we need to describe them in an informative way. The description should be consise, but clear, so that we can easily identify the stage of the project from the moment of that particular commit. What is the purpose of this action? For example, if our code breaks later on, we can quickly decide which commit we want to go back to.

git commit -m "message"

Git Log

We can check the list of all commited changes when we type:

git log
The details we will see are the author of the commit, time and date and a long commit id that is a combination of numbers and letters.

Git Checkout

In order to go back to a version of our project from a certain commit we can type:

git checkout long_commit_id
However, keep in mind that this action will move us to the master branch of the project.

Git Remote

To connect the repository that we created on our local device with a repository on GitHub we need to get the GitHub repo link that we can find on the website once we log in. We can push our commits to a new as well as an existing repository.

git remote add origin https://repository_link.git
We have just created a remote called origin to access the specified repository.

Git Push

This is how we push our commits to the linked repo.

git push -u origin master
This how we commit our changes to the origin repo on branch master.


Working with branches


Listing branches:

git branch
This will give us the list of available branches as well as tell us, which branch we are currently on.

→ Creating a new branch:

git checkout -b new_branch_name
This will create a new branch which will be the exact copy of the current version of our repository.
The newly-created branch will not be a part of the project until we push it to the GitHub repo like so:
git push origin new_branch_name
Now when we print the list of the branches we will see two results: master and new_branch_name.

Deleting branches: To delete a branch locally:

git branch -d local_branch_name
We use the -d option if the branch has already been pushed and merged with the remote branch. We can use -D if that's not the case. To delete a branch remotely:
git push origin --delete remote_branch_name

Moving between branches:

git checkout branch_name

Pulling from branches:

We should always work with the most recent version of the code. In order to do that we need to pull (download) the code from a specific branch of the GitHub repository to our local device.

git pull origin branch_name

Sources:

Illustration for the How to use Git VCS: The Basics

Comments (0)


Be the first to leave a comment