Let's say you're planning a trip with friends and have split up the shopping list, with each person responsible for their portion. It all seems easy and smooth, right? But wait until everyone starts working on their respective portions and has to sync up their updates with the main list frequently. With multiple people doing this, the main list can quickly become corrupted or overlapped with everyone's changes. Before you realize it, things can quickly spiral out of control, and the whole plan can take more effort than intended.
However, there's no need to worry. This is where Git comes in. Git is a version control system that ensures every change is registered and has a history of changes made throughout the process. There are a few terms to consider: Origin refers to the original copy of the file, preferably stored on the remote cloud. Clone means you're trying to pull a copy of the file from the origin to your machine. Once you've cloned the repository - or, in other words, the whole folder where your files live in the cloud - you can finally start making changes. At this point, the version on the origin is different from your cloned version simply because you're making changes to your cloned version.
Once you have made changes to your file on your machine, you double-check that everything looks good and you are ready for your changes to sync-up and publish to the origin remote server. This step is called a git commit. You are simply showing your commitment to the changes you have just made. When you commit, the commit version still lives in your local machine until you explicitly push it out to the origin remote. This is where the Push command comes in. The final step is to push those commits to the origin remote, so your changes make it to the master copy.
This is a basic flow in git; you create and clone a remote origin repository. You will then make changes to the files in the repo and commit to it. Finally, you push it out back to the origin remote. Every commit you make and push out will be registered as in a ledger, so you can always switch back to an older commit. This is especially handy when you mistakenly push out a commit that includes a bug.
In practice, these are the commands that you would use to implement the basic flow in Git and start your version control. As we discussed above, first, you would need to clone down a repo, and we would use the git clone command for that. Remember, a git repo has a dot git extension.
→ git clone https://<some_url>/<repo_name>.git
The next step is to start making changes; let’s say you have a file called “myList.txt” in the cloned repo, and now you can make all the changes you want to the file. Once you have saved the file, the next step is to re-think your changes and ask yourself if you are ready to commit the changes. If yes, you would simply add the file to the commit list using the git add command.
→ git add myList.txt
After this, you run the commit command, which will pick up the files you have added to the commit list from the previous step.
→ git commit -m “my changes to the shopping list”
At this step, a commit has been created locally on your machine, and you would finally want this change to be registered on the origin remote so that anyone else waiting for your shopping list can clone it down. We must run the git push command that syncs your local master version to the origin remote’s master version.
→ git push origin master
And you are done! Every time someone clones the origin master repo, they will get your changes, too. This is a very simple git flow that starts a version control system for your shopping list. Because this is an episode on how git works and what it is, we didn’t cover more advanced concepts like branches, merges, and re-bases. However, the concepts from this episode will get you started with git and make you more comfortable researching.
You may also ask what is GitHub. Well GitHub is a hosted service on the internet that implements the concept of git. You can create a repo in GitHub, and that becomes your origin. You then follow the rest of the process from your local machine. GitHub is a web-based implementation of the git platform where you can create a git-based workflow easily and is home to most open-source projects. For example, the codebase for the Linux Kernel is hosted in GitHub because it makes it easier for thousands of developers worldwide to contribute to the project together.
Please like and subscribe for more git concepts later.