Step By Step Guide To Git Version Control
Version Control Systems(VCS), also known as source code management are software tools that give multiple developers and team members a platform to work together on the same project and track and manage changes to the software code, as much as we have many VCS tools out there, Git remains, without doubt, the most common VCS tools as it is known for great performance, functionality, and flexibility.
What Is Git?
Git is simply an open-source version control system tool that allows individuals/teams to store, track and manage their source code.
Now Let’s Get To Using Git
Below are the first two steps to take to be able to use Git effectively.
Download Git
Click Here To Download Git
Create A GitHub Account
Oh wait, I know you are probably thinking, what is “GitHub” again and NO it’s not the same as Git but they work together.
So What Is GitHub?
GitHub is a cloud-based hosting platform for version control and collaboration, it lets you manage your Git repositories.
Click Here To Create GitHub Account
As much as we won’t be going through every Git command in this article, I will walk you through basic commands with practical examples to make it clearer.
The steps are as follows in this order:
- Creating a local Git repository
- Staging and Committing the code
- Git Status
- Branches
- Merging
- Remote Git Repository
Creating A Local Git Repository
- From your Git bash create a folder(Directory) on your Desktop where it can be visible and easily accessed and name the folder “my_git” using the command:
mkdir my_git
mkdir allows users to create or make new directories and it stands for make directory.
- Go into the new project file you created using the command:
cd my_git
Cd is used to change directories and it stands for Change directory.
- Create a local Git repository in the Project folder using the command:
git init
Git is used to initializing a new git repository or reinitialize an existing one.
- Create a file named practice.txt using the command:
touch practice.txt
Touch is used to create files.
- Open a file with a text editor using the command nano practice.txt and type “it’s time to learn about cloud”, then press “CTRL z” to write and quit the text editor.
nano practice.txt
Nano is easy to learn and uses a terminal-based text editor.
Staging And Committing The Code
Staging in Git means you have to add every file and change it to the staging area for it to be committed.
Now below are the commands to add your file or changes to the staging area.
git add practice.txt
To add all your files to the staging area at once.
git add.
Commit is a command that saves all currently staged changes of the project.
Use the following commands to commit
-m "first commit"
The option “m” in this command means message and “first change” is a commit message to indicate whatever changes you made and make it easier for others to track.
Git Status
Git status is a command used to display the status of the staging area and the local repository.
git status
Branches
Branch in Git means a new/separate version of the main repository(master branch).
Okay I know right now you are thinking why do we need a new/separate branch when the master branch already exists by default, well Git Branching allows you to diverge from the production version (master branch) of code to fix a bug or add a feature until you test and then merge to the master branch.
Creating a new branch
Now you know what a branch is, go ahead and create a new branch and switch into the new branch using the below command:
git branch -B testing
Once you run the command you will be in the new branch, “testing” here is the name of your new branch.
You can run the command Git branch to check the list of branches.
git branch
New Commit In The New Branch
Make changes to the file practice.txt, adding a new word “happy!”.
Remember we are in a new branch (testing) so we need to add to the staging area and commit the new changes to the branch.
git add practice.txt
git commit -m "first changes"
Merging
The command Git merge allows you to merge codes from a separate branch into the main branch(master) to integrate the changes.
Firstly you have to switch back to the master branch:
git checkout master
Then run the merge command:
git merge testing
Run the command below and it will display all the merged branches :
git branch ---merged
Remote Git Repository
All the way here we have only been working in the local repository, as much as individuals will work in their local repositories they will still have to push the codes into a remote repository like GitHub.
By now I’m sure you have a GitHub account ready, now get into your GitHub account and click and click on start a project to create a new Git repository with the name my_git_practice then click on “create repository”.
Now you have a remote repository on GitHub, proceed to open your GitHub repository then copy the URL.
As you can see below, the part marked with red is your URL so now copy it.
so to add your local repository to your remote repository use the command below:
git remote add origin[paste the URL you copied here]
Git Push And Git Pull
Use the below commands to push the codes from the local repository to the remote repository.
git push -u origin master
And to clone an existing project from the remote repository to the local repository on your computer, firstly open the project and click on clone then copy the URL.
Use the below commands to clone :
git clone [paste the URL of the existing project copied here]
I’m happy you read through this tutorial to the end and by now I’m so confident you can explore more about Git on your own.
Don’t forget you can reach out to me on Twitter and please leave a clap if you find this article helpful.