Thomas Rayner shared his six Git commands for beginners in a blog post, in this post I share my journey of learning Git, the commands I use and how you get involved further.
It’s one of those tools, products that I always resigned to being within the developer toolbox and not something an IT Pro would ever consider using. But over the last three years, it’s become one of those tools I keep installed on my machine and one I now use on a weekly basis.
What is Git
Git is an open source project, that has become the de facto standard tool for helping end users/organisations with their version control. It can run on a range of operating systems and there is a large community behind it with lots of documentation and tutorial available.
Learning Git
Now Git is hard to learn. I think that’s a widely known or accepted fact, and I can vouch for it. I only know a sub section of its functionality and tricks. I probably do things in the less effective method possible, but I have learnt how to use it, and as I say it’s become something I use at least once a week.
Why learn Git?
As I said earlier it’s become a de facto standard, for hobbyists to enterprises. Regardless of where your source code is stored, Git can help you manage it. And it’s not just a developer tool, it’s a tool everyone should have at least a basic understanding of.
Getting started with Git
My journey with Git began when I wanted to contribute to Microsoft Docs. Microsoft Docs is open sourced, meaning anyone can contribute to them and Github is used to store the documentation and act as the source control tool.
So, let’s look at how you can start using Git and start your journey to contributing to Microsoft Docs and other open source projects.
Install Git
The first thing you need to do is install Git onto your machine. Depending on what operating system you are using there are different installation methods, this guide can help you through the right method for your system.
Configure Git
Once you have Git installed you need to configure it. You need to set your name and contact information, so that when you contribute to any project so others can identify that it was that made the change.
You do this by issuing to commands:
git config --global user.name "Sarah Lean"
git config --global user.email sarah@fabrikam.com
Work on a repository
I’m going to start work on a Github repository, but rather than do that on the Github website I want to have a copy on my machine to work on until I am happy to share my work.
To pull down a copy of the repository to my machine, I open up a internet browser and find the repository and copy the URL.
I open up my Git session and issue the command:
git clone https://github.com/weeyin83/LearningGit
You now want to change directory and start to work with that repository and it’s files.
cd LearningGit
Now my repository is empty so I want to create a Readme file that will help people understand the purpose of my repository. The readme will act as documentation for the repository and help people understand why I’ve created and how to use it.
I’m going to still to creating and editing my file within the Git console, to show you the full power of Git but there are other methods of creating and modifying files, via tools such as Visual Studio Code.
So, within my Git session, I will issue the command:
cat > readme.md
This creates my new file and then allows me to add in some input.
Once I’ve entered the file contents I want I use Ctrl+D to exit and the file is created and saved.
Now I want to share this file with the rest of my team. To do that I need to do to three things, add the content to the repository index, commit my change to the repository and lastly take the changes I’ve made on my machine and add them to the repository back on Github.
So my first command in the Git session is:
git add readme.md
My new file is now indexed and ready to be committed to my repository. My next command is:
git commit -m “Add in readme”
The commit command records the changes I’ve made and using the -m switch I give a brief description of what I’ve done, again this is best practice so people understand the changes that have been made.
And the last command I issue is:
git push
The push command takes the files and changes you’ve made on your local machine and puts them back into Github.
When you visit Github, you should now see the changes you’ve just committed and push to your repository.
As you can hopefully see using Git can be straightforward.
Git commands I use most
git add . – rather than specifying the individual files I want to update I this command which adds all the files I’ve added, deleted or modified to the index
git commit -m “making changes” – this is committing my changes to the repository
git push – pushing my changes from my local machine to where the central copy of the repository is being stored
git pull – updating my local machine with changes from the central copy of the repository
git clone – pulling the initial copy of a repository down to work on my local machine
git gc – this is the garbage collection command, it runs some housekeeping on the repository such as compressing file review or removing unreachable objects
Getting involved
As I mentioned the Microsoft Docs are open source, so anyone can contribute to the betterment of them, and this was where I started. By seeing some incorrect documentation and correcting it.
It’s also worth mentioning that as we are in October, this year’s current Hacktoberfest is in full swing. This is a community event where people are encouraged to contribute to open source projects and in return can get some awesome swag. You just need to register and contribute to four open source projects and that swag can be yours!
Go out and start your Git journey today!