Git is one of the most popular version control systems out today. With network automation and scripting becoming more and more popular, there needs to be a way to store and track changes made to scripts and configuration files. Not only will Git allow you to do that, but it can also help collaborate with colleagues (via a private Git system) or with the rest of the world (GitHub). GitHub is a site that you can go to and upload your own creations, as well as browse through the work of others. GitHub is built on Git. Let’s go ahead and dive right into the world of Git and GitHub. First, I will upload the code I created in my last post, and after that I will download some code written by Ivan Pepelnjak and shared on his GitHub page.
Git is a system created for version control and collaboration of files/projects. It was created by Linus Torvalds and others in order to efficiently collaborate on development of the Linux kernel. Git can be used to either store and share your own code or projects, or it can be used for many individuals to contribute and develop together on a project.
In Git, a repository is the place to keep all of the files associated with a project. Let’s get started by creating our first repository. We will be utilizing GitHub. First go to the GitHub website. You will immediately be greeted with a screen to create your own account. Fill out a username, e-mail address, and password. Once you have an account and can login, you will see the following page:
It’s not a bad idea to read the guide to get a little familiar with GitHub, but since it focuses on the GUI I will not be going through it in his post. Click on Start a project to create your first repository!
All you have to do is give the repository a name and click if you want it to be Public of Private. Since this GitHub page will include most of the code I use in my blog posts, I have made mine Public. Once your repository is created, GitHub provides you with step by step instructions on how to first set it up!
Now wait a second. If we are using GitHub to create a repository, why is GitHub telling us we can create a repository from the command line? Well since GitHub is built upon Git, we can install Git on our local machine and use it to connect to our remote repository! This will allow us to incorporate GitHub into automation scripts by providing a way to upload configurations, config change scripts, or diagnostic scripts. Git was created with this scenario in mind – all of the Linux Kernel Developers come from all over the world and need to connect remotely in order to collaborate.
In order for us to start connecting remotely to GitHub, we will need to install Git on our machine. I am using my Fedora Linux Server. Git is included in the official repository, so all I had to do is use dnf to install it.
[otaku@netlab ~]$ <span class="mceItemHidden">sudo</span> dnf install git
Once Git is installed, there are two settings we need to set locally on our machine. Since any good version control and collaboration software needs to be able to record who is making changes, we need to tell Git some information about ourselves. We will setup our name and e-mail. While it’s not too important if you are using GitHub privately, it is important if you will be sharing and collaborating. Using the following CLI commands in order to set up your username and email:
[otaku@netlab ~]$ git <span class="mceItemHidden">config</span> --global user.name "Nate Chmura" [otaku@netlab ~]$ git config --global user.email "email@example.com"
The ‘–global’ switch used in the command will set global settings. You can configure different settings for different repositories. Now that we have Git setup locally, we need to setup a local repository that we can link to the remote one on GitHub. You should first create a folder that you will use for the repository. Because I named my repository “automation-intro” on GitHub, I will create a folder with the same name.
[otaku@netlab NetworkOtaku]$ <span class="mceItemHidden">mkdir</span> automation-intro [otaku@netlab NetworkOtaku]$ cd automation-intro
Now we can walk through the steps outlined on the GitHub website for finishing the creation of our first repository.
The first line is simply creating a README file. This file will typically explain the project that the repository is used for. Echo is simply a command to display text as output, and the double greater than signs (>>) signify that we are going to redirect that output to a file named README.md.
[otaku@netlab automation-intro]$ echo "# automation intro" >> README.md
The next line will initialize, or create, the local repository.
[otaku@netlab automation-intro]$ git init
Next we will tell Git to track the README.md file. Tracking is essentially putting files into a queue that will be committed (or added) to the actual repository.
[otaku@netlab automation-intro]$ git <span class="mceItemHidden">add</span> README.md
Once the file has been added, you can now commit it to the repository.
[otaku@netlab automation-intro]$ git commit -m "first commit"
The -m switch allows you to add a message that will accompany the change/addition to the repository. It is good to make the description informative, especially when collaborating with others. If you do not use the -m switch, then a text editor will open for you to write your message.
Now when we commit on our local machine, it is only added to the local repository. We must now push the changes to our GitHub repository. Before we can do that, we must connect our local repository to the remote one:
[otaku@netlab automation-intro]$ git remote add origin https://github.com/nschmura/automation-intro
The URL that you use will differ and reflect the username and repository you created on GitHub. The “origin” keyword names the remote repository. This allows us to simply use the origin name when making all future Git pushes to the remote repository.
After adding the remote repository, you can now push the local committed changes to the remote repository:
[otaku@netlab automation-intro]$ git push <span class="mceItemHidden"><span class="hiddenSpellError">-u</span></span> origin master
As you can see we were able to use the ‘origin’ keyword rather than the full URL for our remote repository. The “master” keyword is used to tell Git that this is the master branch. In Git, a repository can have many branches. When first dealing with Git and GitHub, you only have to worry about pushing to the master branch of your repository.
Lets get a quick recap of the commands used:
- git add – queue up a file to be added/changed
- git commit – add the changes to the local repository
- git remote add – link the local repository to the remote one
- git push – push changes from the local repository to the remote one
The only command you won’t have to worry about as much in the future is Git remote. That is because once the remote repository is linked, you can reference it by the name you specified (origin in our case). For future updates/changes, you will only have to use add, commit, and push.
Now that we have set up our local repository and connected it to our remote repository on GitHub, let’s upload the code from last week to show how you can add new files to a repository.
Adding New Files To A Repository
Adding new files to the repository is as simple as putting them in the directory for that repository either by using a GUI file manager or command line commands like “cp” or “mv.” Once you have them in the directory, just follow the add, commit, and push command sequence from before:
Notice how for “git add” I was able to name all of the new files. This is a lot more efficient than having to run a “git add” command for each and every file. Once you have added them, you can them commit them. Here I set the message (-m switch) as “Adding basic Python Jinja2 template & script.” This would allow anyone that I happened to be collaborating with to understand what I had just added to the repository. Finally, running “git push” will sync the objects to the remote repository. If we refresh our GitHub page, we can now see that all of the files are now there.
We have successfully added files from our local machine to our local repository, and then to GitHub. Being able to do this all quickly from the command line can be harnessed later as different types of scripts are created. For instance, if you have your full-blown device configuration script then you could add a few commands to it in order to automatically sync the created config files to a Git or GitHub repository!
Pulling From Other’s Repositories
Because GitHub provides a platform to share your work with others, I feel the other really important but basic use for Git is being able to create a copy of a repository. You will find lots of good examples from fellow network engineers that you can pull down, modify, and use in your environment or just simply to test out new tools that have been created. In Git, this is accomplished by cloning a repository, and it is super simple.
One such GitHub repository is managed by Ivan Pepelnjak. His repository can be found at https://github.com/ipspace/. To demonstrate cloning in Git, I will pull down a copy of his Ansible Examples repository. Ansible is an automation tool that utilizes ssh to connect to remote devices and has become one of the big open source tools being used for network automation. Ansible is a tool that I am currently learning, so lets pull down the whole repository of examples. If you notice at the top right corner of the repository page, there is a big green button that says “clone or download.” A small box will pop up, providing you with the URL for the repository (https://github.com/ipspace/ansible-examples.git). There is also a link to download just a zip file containing the repository. In order for you clone the repository, from the command line you have to use the “git clone” command and the URL of the repository. The following command will pull down Ivan’s Ansible Examples GitHub repository onto your local machine:
[otaku@netlab automation-intro]$ git clone https://github.com/ipspace/ansible-examples.git
If you run the command it will pull down the repository and put it in a folder using the same name as the repository. You can optionally put a different folder name after the URL if you want the repository to be stored in a folder with a different name.
As you can see, Git is a very simply and also powerful tool, especially for collaborating or sharing your own personal projects. GitHub has become an extremely popular site for people from all different fields to collaborate with others. This blog post only went over the very basics of creating a GitHub account, setting up Git on a Linux Machine, and then pushing and pulling repositories to and from GitHub. If you want to dive deeper into Git, be sure to check out Pro Git. It is an amazing book by Scott Chacon and Ben Straub that serves as both a great tutorial and reference guide. Best of all it is available for free online.