Git And Version Control
As a developer or programmer, looking for a way to collaborate with other dev experts, track your code while making changes to them, know who made changes and when the change was made, and push or pull your code online without stress? Then you are in the right corner, chill as you enjoy the cruise.
What is version control?
Version control also known as source control, is the practice of tracking and managing changes made to a file over time. As development environments have accelerated, version control systems help software teams work faster and smarter. They are especially useful for DevOps teams since they help them to reduce development time and increase successful deployments.
Version control software keeps track of every modification to the code in a special kind of database. If a mistake is made, developers can turn back the clock and compare earlier versions of the code to help fix the mistake while minimizing disruption to all team members.
There are three types of version control.
- Local Version Controls
- Centralized Version Controls
- Distributed Version Controls
Local Version Controls
Version database or file is present on a local computer. Multiple people cannot work parallelly on the same project.
Centralized Version Controls
In CVC, a central repository is maintained where all versions are kept and a user can check out and check in files from their different computers at any time. The issue with CVC is that in case of central server failure, the whole system goes down. However, the solution is distributed Version Control.
Distributed Version Controls
Version database or file is stored at every user’s local system and at remote servers. If any of the servers dies, a client-server user can be used to restore the data.
What Is Git?
Git is a version control system that developers use all over the world. It helps you track different versions of your code and collaborate with other developers. Git is basically used for tracking code changes, who made the change and code collaboration.
What Is GitHub?
GitHub is a product that allows you to host your Git projects on a remote server somewhere (or in other words, in the cloud).
It’s important to remember that GitHub is not Git. GitHub is just a hosting service. There are other companies that offer hosting services that do the same thing as GitHub, such as Bitbucket and GitLab.
How To Get Started With Git.
You can either use Git by typing commands in the terminal or you can use a graphical user interface (GUI) such as Sourcetree or GitKraken.
Luckily you don’t have to learn these by heart. Other than a handful of commands that you will use most often, the rest you can look up whenever you need them. Set up Git in the command shell by adding your name and email, this will help Git with the right details as you make changes to codes or collaborate with others.
Major Git Commands To Note.
- Git init Command: this command is used to create or initialize a repository. the git init command makes git aware of the code and file available in the resource.
- Git Status Command: this command tells the state or status of git at every point in time, the changes made, and the branch that git is at the moment. In checking the status of git, some files come as tracked or untracked files.
a. Tracked file: Tracked files are files that were in the last snapshot, as well as any newly staged files; they can be unmodified, modified, or staged. In short, tracked files are files that Git knows about.
b. Untracked file: Untracked files are files that have been created within your repo’s working directory but have not yet been added to the repository’s tracking index using the git add command.
3. Git Add Command: This command is used to add the untracked files to git so that git can start to monitor and record changes made to the file or code. To avoid adding files one after the other in the case where there are plenty of files to be added, use the git add . command to automatically add all the folders in the git.
4. Git Commit Command: The “commit” command is used to save your changes to the local repository.
Get Shaibu Barnabas Anibe’s stories in your inbox
Join Medium for free to get updates from this writer.
Note that you have to explicitly tell Git which changes you want to include in a commit before running the “git commit” command. This means that a file won’t be automatically included in the next commit just because it was changed. Instead, you need to use the “git add” command to mark the desired changes for inclusion.
Also note that in Git (not like in Subversion), a commit is not automatically transferred to the remote server. Using the “git commit” command only saves a new commit object in the local Git repository. Exchanging commits has to be performed manually and explicitly (with the “git fetch”, “git pull”, and “git push” commands). -m <message> Sets the commit’s message. Make sure to provide a concise description that helps your teammates (and yourself) understand what happened.
5. Git Branching Command: The git branch command lets you create, list, rename, and delete branches. It doesn’t let you switch between branches or put a forked history back together again. For this reason, git branch is tightly integrated with the git checkout and git merge commands. This command allows you to make different copies of source code or files and gives you access to make changes to them independently.
6. Git Checkout Command: This command helps you move from one branch to another.
HOW TO USE GITHUB
1. Sign up for GitHub
In order to use GitHub, you’ll need a GitHub account. You can create a free GitHub account and start using GitHub right away. With a free account, you’ll get access to unlimited public and private repositories. You’ll also get bug-tracking and project management features. The only downside is that you’ll only be allowed three collaborators for private repositories. If you’re still in the “GitHub for beginners” stage, signing up for a free account is a great place to start.
2. Install Git
GitHub runs on Git. What is Git? Git is a version-control system used for tracking changes made to codes as earlier stated.
3. Create a Repository
To do anything in GitHub, you’ll need to know how to first start a repository. A repository (or repo) is essentially synonymous with the word “project.” Quite simply, a repository stores everything pertinent to a specific project including files, images, spreadsheets, data sets, and videos, often sorted into files. It’s best to include a README file within your repository that has specific information regarding the given project. On GitHub, you can add a README file right when you create a new repository.
To create a new repository, you’ll hit “new repository” in the upper-right-hand corner. You can then name your repository, include a brief description, and check the box that says “initialize this repository with a README.” Finally, you’ll click “create repository.”
4. Create a Branch
Projects are multi-faceted and many program versions are required when you’re building. Branching enables you to edit multiple unique versions of a repository at once. Your repository automatically has a definitive branch called master. You can work on several different branches in order to make edits before eventually committing them to the master branch.
When a new branch is started, it’ll be a copy of the master branch until you edit it to make new changes. A branch typically goes through many steps and approvals before it is ever merged into the master branch. To start a new branch in GitHub, navigate to your new repository, click the dropdown that reads “branch: master,” type a branch name (like README-edits), and then hit “create a branch.” Branches are ideal for new features or bug fixes.
5. Create and Commit Changes to a Branch
To make changes to a branch in GitHub, go to the code view for your newly created branch. Click the file you want to change, then hit the pencil icon in the upper right, make any necessary edits, describe your changes by writing a commit message, and then click “commit changes.” Each saved change is called a commit. Every individual commit has its own commit message which gives more details into why a specific change was done. The commit messages give a history of changes and help project contributors understand how the project has changed over time.
6. Open A Pull Request
In order for any branch to be merged into another person’s branch, you must open a pull request. A pull request is GitHub’s way of notifying relevant parties about your request to incorporate changes into their branch. A pull request will show in red and green the differences of the content between branches. You can make a pull request any time you complete a commit. For best results, when sending a pull request, you can use the “@” feature to mention specific people from whom you need feedback.
To open a pull request, you’ll go to the “pull request” tab and hit the button that says “new pull request.” Next, in the “example comparisons” box, find the branch you made and compare it with the master. Ensure you like the changes and then click the “create pull request” button. Title your pull request and briefly describe the changes. To finish, click “create pull request.”
7. Merge Your Pull Request
Merging your pull request with the master branch is something you may need to pass on to your superiors to handle. For the sake of learning, though, you can practice doing it yourself. Simply hit the button that says “merge pull request,” select “confirm merge,” and then delete the branch you merged once it has been incorporated into the master.
GitHub WORKFLOW
Here are some good work practices to make your work simple and easy to understand by others in using GitHub.
- Don’t work on your main or master branch because your main or master branch should be presentable all the time, it shouldn’t be a work in progress or carry bugs from unfinished lines of code. The main/master branch should be your source of truth. So, create a new branch and checkout immediately to the new branch before you start working using the command git checkout -b feat/(the name of the file you are working on)
- Always remember to commit when you reach a reasonable milestone and remember to add a descriptive message to your commit to help track what change was made.
- Before pushing a code, is advisable to pull from your main or master branch in case there will be any change made on that branch so that it will be included in your code before you push it online.
- After pushing your code online, create a pull request by pulling your code into another branch. merge your code to the baseline which is the main, then your code which is on the compare line should be merged with the baseline which is the main.
- After creating a pull request, add reviewers. Reviewers are other collaborators that will review your code, and make corrections or comments to make sure it’s free of bugs before it is been added to the main/master branch.
- Finally, merge the pull request for your to come up on the main branch.





