DEV Community

Cover image for Making life a breeze, with Git
Methmi
Methmi

Posted on

Making life a breeze, with Git

Simply put,

Git is a distributed version control system.

Breaking this down shows the essence of Git:

Control System : Git can be used to track and store content stored in repositories.

Version Control System : A history of all changes made to the content is stored. Also, Git allows multiple users to change content parallelly and keep things clean by separating work into branches

Distributed Version Control System : Machines using a Git repository have a local copy of the repository stored and the repository is stored by remotely on a server by Git as well.

To sum up, Git can be used anywhere content will be changed constantly and where it will be helpful to keep track of changes made so that content can be reverted to how it was at a certain point in its history.

These features along with other features such as branching, to run multiple projects with the same base, parallelly, makes Git a very useful tool for storing and updating code.

Creating a Git repository

A repository is where all your code (or any other content) will live. It can start off as an ordinary folder or folder structure and then be converted into a smart Git repository.

For this example, we will assume that an empty folder called ‘my-code-base’ has been created and a terminal window is opened up and the ‘cd’ command is used to navigate into the folder.

cd my-code-base

git init
Enter fullscreen mode Exit fullscreen mode

The git init command turns the folder into a local repository.

Staging and Committing

Assuming that a plain text file called ‘dummy-file.txt’ is added to the folder, let’s stage it, and then commit it.

Staging is preparing a file (or a change in a file) to be put into a change called a commit.

Staging is required before every commit, this is how the file 'dummy-file.txt' will be staged.

git add dummy-file.txt
Enter fullscreen mode Exit fullscreen mode

To stage multiple files at once:

git add dummy-file.txt dummy-file2.txt
Enter fullscreen mode Exit fullscreen mode

Or to stage all files into staging area:

git add .
Enter fullscreen mode Exit fullscreen mode

Committing involves registering all files currently in the staging area as a change to the repository, including a commit message describing the changes made.

git commit -m "Dummy data added to the file"
Enter fullscreen mode Exit fullscreen mode

The -m indicates that the following string wrapped in " " is a commit message. If this is omitted the user is prompted to enter a commit message after running this command.

Another commit option is -a which automatically stages all modified files to be committed. However, this will only automatically stage files that have already been committed to the repository. For example, if file1 which has been committed in a previous commit is modified and file2 is a newly added file which has not been committed, the -a option will only stage and commit file1 because Git is not aware of file2.

git commit -am “New changes”
Enter fullscreen mode Exit fullscreen mode

Status

Status of files including which files have been modified and what files are in the staging area can be revealed with

git status
Enter fullscreen mode Exit fullscreen mode

Branches

Branches are useful for editing the same codebase parallelly without affecting another developer editing the same code. Also, if a mistake is made when editing, this will be kept separated from other branches and the main branch.

A branch can be created with

git branch test-branch
Enter fullscreen mode Exit fullscreen mode

and switch from the master branch (or current branch) to the created branch with

git checkout test-branch
Enter fullscreen mode Exit fullscreen mode

Also, a list of branches created can be shown with

git branch
Enter fullscreen mode Exit fullscreen mode

Now, new changes can be staged and committed to this branch using the previous commit and stage commands.

Merge

This is used to join a separate branch back into the active branch.

First, checkout into the branch that the other branch needs to be merged into.

git checkout master
Enter fullscreen mode Exit fullscreen mode

Then run

git merge test-branch
Enter fullscreen mode Exit fullscreen mode

and the branch test-branch will be merged into the branch you are currently on.

Managing a remote repository

The repository made so far exists in the local scope, this repository can be stored in remote server that can allow other developers to access and clone the repository.

GitHub is a popular repository hosting service, and for this example the remote repository will be hosted in GitHub.

After logging into a GitHub account, a new repository can be created via the menu on the top-righthand corner.

Image description

Next, a name and privacy setting for the repository can be chosen.

Image description

After setting up the repository, locate the repository’s URL on its page.

Image description

Next, this URL can be used to form the command that points the local repository to the remote repository.

git remote add origin [repository-URL]
Enter fullscreen mode Exit fullscreen mode

Git Push

A push involves sending changes from a local repository to its remote repository.

To push changes, the following command is used

git push -u origin master
Enter fullscreen mode Exit fullscreen mode

This pushes the committed changes in the master branch of the local repository to the master branch of the remote repository.

Git Pull

A pull denotes updating the local repository with latest changes in the remote repository.

Latest code of a certain branch (the master branch, in this example) of a remote repository can be pulled by

git pull origin master
Enter fullscreen mode Exit fullscreen mode

Create a working copy of a repository ( Checkout repository )

A locally stored repository repository can be cloned by running the command:

git clone path/to/local/repo
Enter fullscreen mode Exit fullscreen mode

And, for a repository stored in a remote server:

git clone <<repository-url>>
Enter fullscreen mode Exit fullscreen mode

And that is how Git can be used to make versioning easy and clean and to make collaboration a breeze!

Till next time, happy coding!

Top comments (0)