Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Wednesday, April 30, 2014

Git From The Command Line

After spending a few years being involved with the Linux community, one of the items that has always amazed me is the number of people who still are not sure how to use git from the command line. There are a number of GUI programs available to make this process easier for those who are uncomfortable using the CLI, but in reality the process of editing and pushing a program from one’s computer to the Github repositories is not very difficult when someone takes the time to show others how to go about the basic steps.

In this tutorial, I’m going to cover the basics of how to open a program using Vim, make a small change to that program, save it then push the changed program up to the Github repository. All of this will be done from using the terminal so no extra programs need to be used. This is often the working scenario for people who work in a GUI-less environment or have to interface with headless servers. I’m taking into account that the person following this tutorial has already followed the instructions of setting up a Github account, repository and established an ssh key pair with their account. The instructions for setting up git are easy to follow and can be located here:  https://help.github.com/articles/set-up-git.

Once a remote account and repository has been established then connected to a local directory, the rest is simply going through the motions of making it all work. For starters, open a terminal session such as Konsole, X-Term or whatever comes with the operating system.

start.png

Once the terminal is up and going then simply navigate to the directory that was created specifically for communicating with the established Github repository using git.

cd_to_git_dir.png

Now that we are working inside the established git directory, the next thing to do is open the particular program we are going to be working with. For this example, we are simply going to open the all-too-familiar “Hello World” starter using the Vi or (Vim) editor. Vim runs directly in the terminal rather than opening a separate program. This makes life much easier as we don’t have to leave the environment we are already working in. To open Vim and start working, just type in the following command:

start_vim.png

Once this is executed, the terminal will automatically switch over to the Vim editor for making any necessary changes to the program.

editing_a_program_with_vim.png

Remember this is all happening without starting any external programs! Make any changes to the program using the Vim editor then save those changes. When the changes have been saved, Vim will automatically close and bring the user back to the command line where they left off.

The next step is to get a status report from git that shows us what has changed from the last time we accessed our local repository or git directory. The command to see any changes made is “git status”.

input_git_status.png

When this command is invoked, git is going to give a response that shows all the changes that have been made to any programs in the git directory we are currently working in.

message_from_git_after_running_status.png

In this case, we only modified one program and that modification is being reflected here in red text. If we had been busier and had multiple changes to many programs all in the same git directory, all those files would show in the list with hint notes of suggested actions based on the action to to do next.

In this basic example, we are simply going to invoke the command “git add” which is similar to clicking on a checkbox in a GUI and selecting the file to be added to the next operation. If there were more than one file, we could add one or a few to be added to our next commit which will get a specific message attached to it that lets others know what changes were made in case an entire group is working on the project the file is associated with.

input_git_add_to add_file_for_pushing.png

Once the “git add” command is executed, git will not return any acknowledgement of what has been added unless the command is executed incorrectly. Sometimes I believe it would be nice to receive a confirmation of what has been added to our list but, I’m not a developer for git so they decide what is relevant and what isn’t.

Once all of the files to be added to the current commit have been added to our list of files, it is time to commit them to be pushed up to the remote repository. Git strongly suggests that comments be added to any commit as it is part of the “essence” of collaboration using a version control system (VCS). To make this process easy, we invoke the “-m” to our command and add a message to be associated with the file’s commit such as below.

input_git_commit_string_with_memo.png

The string contained in the quotes is the message to be added to our commit. This message will accompany all the files that were included in the “git add” process. As such, if we want to have a separate message associated with multiple commits, then only add certain files at a time that will receive any particular commit message. If for any reason the “-m” is left out of the commit syntax, the terminal editing program”nano” will execute by default and ask for the message to be attached to the commit. It’s much easier to just remember to include the “-m” in my opinion unless a person prefers the added step of using the nano editor.

When the commit command is executed, a message similar to the following will be displayed from git:

git_commit_return_message.png

This message just reiterates the changes that were made to the file, the commit message that will be added to the commit as well as the relevant version control numbers that will be associated with the commit when pushed up. It should also be noted that it is completely acceptable to stop at this point and go work on something else. Each command is it’s own entity and therefore do not need to be executed immediately. For example, a person may want to only push all their changes to the remote repository on Friday afternoon or on Monday morning. Or, a person may simply run a cron job to have all their pushes done on a certain day of the week at a certain time automatically.

For this example though, we’re going to go through each step individually so that we get the basics of what’s going on before jumping into ideas of batch pushes or cron jobs. : )

The next operation to perform is just simply execute the command “push” like shown below:

input_push_file_to_remote_repository.png

Since this entire time we have been working in our established git local repository, the “push” command is going to push our local changes to our remote server repository located at Github. This how we share our work with others so we can all collaborate on the same project while working from anywhere. Once the “push” command is executed (in most circumstances) the following message will appear:

input_password_for_ssh_key.png

What this message is asking for is a password associated with a registered ssh key at the remote end. Having an ssh key pair is not a requirement to use Github VCS although, it should be noted that it is an excellent security and identity measure to verify that the changes being made in the repository are actually coming from you and not someone else. An ssh key pair is like a fingerprint that distinguishes a person and the computer they work on from everyone else in the world. Even if a person were to try and imposter someone from a different terminal, without the private key being on their machine to match with the public key registered at Github, the push would not be accepted. As such, a person needs to understand that using key pairs is beneficial from a security point of view but can also be a hindrance if working from a foreign machine.

From this point we type in our associated password that identifies us from everyone else and allows the ssh tunnel to push our code from our machine to the remote server securely and encrypted. When finished, git will give us a nice confirmation message that lets us know everything was completed as expected.

git_message_after_pushing.png

At this point we have finished editing our file and pushing it up to our remote repository and accomplished all of this without having to leave the terminal for any reason. The next step is to verify that our changes did in fact make it to their intended destination which involves opening a browser and navigating over to our Github account.

Once we do that, we should be able to go to the repository that is associated with our local git directory and find the file in question as well as the commit message we attached to that file.

github_verification.png

As can be seen from this snippet image of the Github repository line, the file “hello_world.pl” was received and in the center is the commit message we added using the “-m” on thecommand line. Navigating the various links on Github will provide further details of our commit including the actual code and all associated changes highlighted for ourselves or others to refer to later.

I hope this tutorial has explained in detail the simplicity of using the command line to both edit files and push those edited files to the Github version control all done from the command line.

Tuesday, September 4, 2012

Smart, Git and Easy

 Here’s a great program that my friend Andy from openSUSE turned me on to that makes handling Git that much easier than typing at the CLI (Command Line Interface). Many Linux users are already familiar with Git, but for those who aren’t, Git is a nice revision control sharing system. A host is used as an interface for the commands and in a similar fashion as Google Drive or Dropbox, files are moved in and out. Git provides timestamps to each file as it is accessed, or in Gits case pushed into the host or pulled from the host. This file sharing method makes collaborating on a single file or a multitude of files that much easier to track so everyone involved with the file can tell which file was updated last so there is no confusion. At least this is my jist of Git.

 SmartGit is a really nice GUI for taking place of doing Git commands from the terminal line. I grew up in the pre-GUI age long before Windows was invented and spent years and years working from the command line. To be honest, if I never had to use one again would be ok with me, but as most Linux distros and programmers have not fully reached a point of escape from command line operations, sometimes we need to go there. Thankfully there are programmers who think like I do and work their magic on projects like SmartGit to eliminate some of the old CLI usage.

 I would have preferred SmartGit to be a little easier to get setup, but a couple of good articles via Google search and it can be done fairly easy. It downloads as a tar file but doesn’t get extracted and installed the same way most would. Instead, the files simply get extracted and then some modifications have to be made. If it weren’t for the fact that this software came highly recommended by someone I trust, I might have looked for an easier alternative or gave in and went by command line, but I kind of like a challenge anyway.

The first thing to note is that this GUI is dependent on the Java Runtime Environment, so make sure it is installed on your computer or at least have the file available. We are going to have to know the path to the file and paste it into one of the SmartGit files so it knows where to get it from.

This is the file we will need to open with our text editor and put in the path to our JRE.


Next, with text editor of choice, insert the path location into the file and save it. It should look like this.


Now all we have to do is run it and then add a launcher so we don’t have to initialize from the command line all the time. Open a terminal session and switch to the directory where SmartGit has been extracted then initialize it like a C++ program (or similar) like this.



If for any reason SmartGit does not initialize, go back and make sure the path to the JRE is correct and all terminal entries are correct. If all went well then we should be looking at the SmartGit start screen. To install the quick launcher just repeat the steps in the terminal but this time use the add-menuitem.sh file and it will put an icon on the desktop for you.


On the initial start up, there will be an option for where the Git account to associate with is located. This is something that will either be already set up or will need to be. The rest is to just select all the default values given. Once all that is done, then just work your way around the interface and relax because all those typing lines just became simple mouse clicks. Fast, fun and easy just the way we like it. : ) 

REMEMBER: Git is best used for programming files when revision control is really important. It CAN be used as a replacement for Google Drive or Dropbox, but it’s not recommended and even the GitHub website states that it is NOT intended to be used in the same capacity, hence the reason they only allocate 1G to each account for free.