You may have come across a scenario where you got an updated version of some software released by its organization, but soon after release, its performance started decreasing, and users started facing issues with the version, so the company rolled back the newer version and came up with the previous version. So that means they would have stored this previous version somewhere before building and releasing a new version of the software, so this is what is called version control, which is controlling different versions of your code (version is just a change in our code). We have a lot of version control software in the industry, but the most popular is the 'git version control'.
Let's dive in to understand how to control versions(changes made in code) using Git and Git Hub.
Firstly install git in your system.
Before starting, first, open your Project in your code editor and open the terminal.
Step 1: Create repo: To save the changes and roll back between we need to first have a repository. So the command to initialize the repository is
git init
Step 2: Check the Status of your files. This will display the files in which we made changes or added new files but not added to the new version. We can say it will list untracked/unstaged files.
git status
Step 3: Add untracked files to the repo.
git add .
Step 4: Now we have tracked all changes now we want to save this repo. So the command is
git commit -m “<message>”
here -m is to specify that after -m there will be a message which will be stored with this commit.
Step 5: Now add a file to your project or you can do any changes in the current program like adding a print line (System.out.print in java).
Step 6: Now as we’ve made changes so this is a new version of our project, so again perform Step 2,3,4 in the same order. Make sure while commit you give a different message this time.
You can see your new version is pushed to git. Let's now try rolling back the older version.
Step 7: To rollback first we have to see how many older versions we have and which version we need to roll back, so for this purpose, we have
git log
The log will display all the older commits with commit id in yellow having a series combination of 40 characters and numbers. You copy the commit id which we want to roll back to.
Step 8: Its rollback point
git checkout <commit id>
paste the copied commit id after the checkout keyword, you will see the older version of the project will be stored automatically.
Now you have learned to roll back the commit, you can also do vice versa that is, you can return go back to a newer version by copying its commit id.
Till now we learned git. Note that git is software that is present in your local machine, now we also have to put it on the internet so that anyone can see the code and the commits you made, there comes GitHub into the picture. GitHub is used to share code online so anyone can access it easily.
Go to github.com and create your account.
Click on create Repository - Add Repo Name.
In the same window click on Create at the bottom.
Copy the HTTPS link from the quick setup.
Now we have our project ready in git on our local machine, and also GitHub is set up, we have to connect both.
Open your project terminal, and type this command. Paste the copied link after the origin keyword.
git remote add origin <https github repo link>
Congratulations. We have linked out git and GitHub, now we just need to upload our project to GitHub which is on our local machine.
To push the project use the following command.
git push -u origin master
So here we have learned to use Git and GitHub, to establish connections between them and push our project to GitHub.
However there is a lot more to learn to be a pro, but we aimed to understand Git, Github and its use. I will cover more on GitHub like how to pull, merge, fork, etc in the upcoming blogs.
Thanks for reading. Share it with your peers if you loved reading.