Git — Introduction to Version Control
I’ld like to tell you about a wonderful collaboration tool for developers called
Git. It’s so cool that it is (I believe) the number 1 version control system, widely used by developers around the world.
Difference between Git & Github
Git is a version control system; think of it as a series of snapshots (commits) of your code. You see a path of these snapshots, in which order they were created. You can make branches to experiment and come back to snapshots you took.
GitHub, is a web application on which you can publish your Git repositories and collaborate with other people. — StackOverflow
What does Git have to offer me?
A very powerful version control system which makes collaboration easy and helps the developer focus on coding rather that figuring out how to go back to previous version, to add or remove features.
For example, Mary & David are collaborating on a school project, let’s say setting examination questions for different subjects. If they didn’t make use of version control, Mary would have to manually update her working directory with David’s by copying his current working directory to her’s which might result in the loss of her files too due to being overwritten by David’s. Also if there is a need to go back to where they were two days ago, it would be very difficult for either of them. Version control solve this problems and more.
Getting Started With Git
A developer can use Git either by making use of a desktop client or via Terminal. The best way to leverage Git’s power is to make use of it via your PC’s terminal, that way you have complete control over version control and you become “bad-ass”.
Here are some of the advantages of using Git via command line interface:
- It’s independent from the repositories, so it’s doesn’t matter if you are working with a Github repository, Bitbucket repository or other git service.
- Have all of awesome functionalities of Git (none of the actual GUI apps has that).
- When you’re developing using the command line, having version control right there in the terminal is convenient.
- The Git CLI offers a greater amount of control with respect to branching and merging.
- If you use Git with services other than Github (for example, for pushing your code to production), the GUI client may make that difficult/impossible. — Quora
At this point, I’ld like to re-iterate the difference between Git and Github again. You should think of Git as a tool and GitHub as a website that makes it easy for us to use that tool.
Git can be used on any of the popular OS:
We’ll start with configuring Git first. Once you’re done downloading Git on your system, Git needs a way to know who you are and what account contains your repositories. This is easy and details are provided here. You can configure Git globally or just for a single repository. To set up Git globally means every git command will be in tandem with the Git account you’ve configured here.
To configure, you’ll have to set a name and email address that your PC should be associated with. It is advisable to use that which you used in creating your github account so that git will automatically link your name to your profile when someone looks at the commits on Github. Let’s assume we want to configure Git for a developer named “Bayo Emeka,” here’s how we would successfully configure git on his PC (be it Mac, Linux or windows — these commands don’t change). First, you start up your terminal then you run these commands:
git config --global user.name "Bayo Emeka" is used to set the user’s name.
git config --global user.email "email@example.com sets the user’s email address which in this case was used in creating a github account.
P.S: We can also configure Git for BitBucket, Mercurial and other version control repositories.
Once this is done, we can now make use of the git commands and work with new and existing repositories.
Starting a new project
Once git is set up, starting a new project and making use of version control is so simple. The command:
git init initializes git in the current working directory by adding a
.git directory that contains the configuration of the user and project too.
Here is a diagram depicting a simple, git workflow when it comes to branching:
A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master. A branch can be likened to a new instance or version of your project which can be experimented on without affecting any of the previous state of the project.
git branch branch-name is used to create a new branch, where “branch-name” represents the name of the branch you want to create.
The process of moving to a branch is known as
checking out.You need to have an existing branch to be able to branch out there, but you can create one while checking out.
To checkout to an existing branch, the command
git checkout branch-name is used while to checkout to a new branch, the command
git checkout -b branch-name is used instead. The only difference between the two commands is
Cloning a repository
Cloning as the name implies means creating a local copy of an online repository. Look at it as the act of
copy and pasting an online repo to your PC and have access to the contents of the repository. To clone a repository, you need to have the repository link. For example, let’s assume i’ll like to clone this repository: https://github.com/andela-bolajide/BucketListAPI
git clone https://github.com/andela-bolajide/BucketListAPI does the trick for me.
Adding & Committing Changes
When changes are made to an existing file or a new one is created, the version control needs to be notified of those changes. This is done via adding and committing those changes. The command
git add file_name is used to update the index using the current content found in the working tree, to prepare the content staged for the next commit.
Committing is simply recording changes to the repository. The command
git commit -m "insert commit message here" is used to commit files that have been added.
P.S: You have to add your changes before you can commit them.
We’ll be going over more git commands and the wonder that is git.
Feel free to ask questions in the comment section or via my twitter handle: @cooproton.