No one codes alone, we need to be involved in a team to build something great. We have tools like Google docs to write collaboratively, how about code? Do we have something to ease us to code collaboratively?

Introducing, Git.

By definition, Git is a

What is exactly a version control?

Version control is the practice of managing and tracking changes to the software code. Version control keeps track of every modification to the code in a special kind of database, so you can go back to the previous version of your code and your teammates can see and track the changes that you’ve made in the code so there won’t be a miscommunication.

It’s like continuously make different versions of your work, incrementally, without having to actually save different versions of your code file.

Your friend wants to see the previous version of the code you’ve been changing? Can.

You want to go back to the previous version of your code because you noticed that you’ve been doing things wrong? Can.

You want to combine the code you and your friend have written without having to manually compare it side by side? Can.

How is Git different from other version control systems (VCS)?

The major difference between Git and any other VCS is the way Git thinks about its data. Most other systems store information as a list of file-based changes. So it saves the different versions of the file stored, pictured in the graph below:

How other systems store data. (https://git-scm.com/book/en/v2/Getting-Started-What-is-Git%3F)

On the other hand, Git stores its data more like a series of a miniature filesystem. Everytime you commit/save the state of your project, Git takes a picture of what all your files look like at the moment and stores a reference to the snapshot.

How git store data as snapshots (https://git-scm.com/book/en/v2/Getting-Started-What-is-Git%3F)

…enough about what is Git, now let’s jump into how to Git

To operate in Git, we need to acknowledge several commands, I’m going to give screenshots about how I implement it in my current/previous projects.

Tell Git who you are

Before using Git, you need to set up your account that you want to use first, set up your username or email. This username and email will be used in the commits, so that git knows who write/update certain part of a code. How? Use git config.

git config --global user.name "Sam Smith" #set up username
git config --global user.email sam@example.com #set up email

After you do that, you can use

git config --list #to list all the settings
git config user.name #to see the username used in commits
git config user.email #to see the email used in commits

to list the email and usernames of git stored in your PC. Mine looks like this:

Create a new local repository

Let’s say you want to start a new project that will use git as its VCS, all you need to do is to run

git init

Create a working copy of a remote server repository

Let’s say you want to contribute to a project called raisingstar that is located in https://gitlab.com/Kyosena/raisingstar.

You want to copy this repository to your local folder, all you have to do is to run:

git clone username@host:/path/to/repository

Then, the repository will be copied to your folder, like this:

Add files into the staging

If you want to add files to the staging, simply put

git add <filename> # to add a certain file to the staging
git add * # to add all files to the staging

Commit

After changing things in your code, you can commit your changes by this comment:

git commit -m "Commit message"

Commit message needs to be descriptive to the changes you’ve made in that particular commit.

Here is example adding files and commits it in the project:

Push

After you commit your changes, you can send changes to your online repository, using this command:

git push origin <the-name-of-the-branch>

Status

If you want to know the status of the git, you can use

git status

Branching

A branch is a feature that is used to reduce code conflict with other members. Here are some branches in our project:

Here are the commands that are used regarding branches:

git checkout -b <branchname> #switch to a new branch
git checkout <branchname> #switch from one branch to another
git branch #list all the branches in your repo
git branch -d <branchname> #delete the feature branch
git push origin <branchname> #push the branch to your remote repository
git push --all origin #push all branches to your remote repository
git push origin :<branchname> #delete a branch

Update from remote repository

To update from remote repository, you can use these commands:

git pull # Fetch and merge changes on the remote server
git merge <branchname> # merge between branches
git diff # View all the merge conflicts

Git Merge vs Git Rebase

git rebase solves the same problem as git merge. Both of these commands are designed to integrate changes from one branch into another branch—they just do it in very different ways.

To know the difference, let’s see a case, suppose you start working on a new feature in a dedicated branch, then another team member updates the master branch with new commits.

source: https://www.atlassian.com/git/tutorials/merging-vs-rebasing

To incorporate the new commits into your feature branch, you have two options: merging or rebasing.

Merge

To do merge, you can use command:

git merge feature master

This creates a new “merge commit” in the feature branch that ties together the histories of both branches, giving you a branch structure that looks like this:

source: https://www.atlassian.com/git/tutorials/merging-vs-rebasing

Rebase

As an alternative to merging, you can rebase the feature branch onto master branch using the following commands:

git checkout feature
git rebase master

Instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.

source: https://www.atlassian.com/git/tutorials/merging-vs-rebasing

Hope this short explanation about git can help you start, thank you for reading! 😻

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store