Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
An Introduction to Using Git

#1
An Introduction to Using Git

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/an-introduction-to-using-git.png" width="1920" height="1045" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/an-introduction-to-using-git.png" class="ff-og-image-inserted" /></div>
<p>If you’re a developer, then you know your way around development tools. You’ve spent years studying one or more programming languages and have perfected your skills. You can develop with GUI tools or from the command line. On your own, nothing can stop you. You code as if your mind and your fingers are one to create elegant, perfectly commented, source for an app you know will take the world by storm.</p>
<p>But what happens when you’re tasked with collaborating on a project? Or what about when that app you’ve developed becomes bigger than just you? What’s the next step? If you want to successfully collaborate with other developers, you’ll want to make use of a distributed version control system. With such a system, collaborating on a project becomes incredibly efficient and reliable. One such system is <a href="https://git-scm.com/">Git</a>. Along with Git comes a handy repository called <a href="https://github.com/">GitHub</a>, where you can house your projects, such that a team can check out and check in code.</p>
<p>I will walk you through the very basics of getting Git up and running and using it with GitHub, so the development on your game-changing app can be taken to the next level. I’ll be demonstrating on Ubuntu 18.04, so if your distribution of choice is different, you’ll only need to modify the Git install commands to suit your distribution’s package manager.</p>
<h3>Git and GitHub</h3>
<p>The first thing to do is create a free GitHub account. Head over to the <a href="https://github.com/join?source=header-home">GitHub signup page</a> and fill out the necessary information. Once you’ve done that, you’re ready to move on to installing Git (you can actually do these two steps in any order).</p>
<p>Installing Git is simple. Open up a terminal window and issue the command:</p>
<pre>
sudo apt install git-all</pre>
<p>This will include a rather large number of dependencies, but you’ll wind up with everything you need to work with Git and GitHub.</p>
<p>On a side note: I use Git quite a bit to download source for application installation. There are times when a piece of software isn’t available via the built-in package manager. Instead of downloading the source files from a third-party location, I’ll often go the project’s Git page and clone the package like so:</p>
<pre>
git clone ADDRESS</pre>
<p>Where ADDRESS is the URL given on the software’s Git page.<br class="kix-line-break" />Doing this most always ensures I am installing the latest release of a package.</p>
<p>Create a local repository and add a file</p>
<p>The next step is to create a local repository on your system (we’ll call it <em>newproject</em><em> </em>and house it in <em>~/</em>). Open up a terminal window and issue the commands:</p>
<pre>
cd ~/ mkdir newproject cd newproject</pre>
<p>Now we must initialize the repository. In the<em> </em><em>~/newproject</em> folder, issue the command <em>git init</em><em>.</em> When the command completes, you should see that the empty Git repository has been created (Figure 1).</p>
<p>Next we need to add a file to the project. From within the root folder (~/newproject) issue the command:</p>
<pre>
touch readme.txt</pre>
<p>You will now have an empty file in your repository. Issue the command git status to verify that Git is aware of the new file (Figure 2).</p>
<p>Even though Git is aware of the file, it hasn’t actually been added to the project. To do that, issue the command:</p>
<pre>
git add readme.txt</pre>
<p>Once you’ve done that, issue the<em> </em><em>git status </em>command again to see that readme.txt is now considered a new file in the project (Figure 3).</p>
<h3>Your first commit</h3>
<p>With the new file in the staging environment, you are now ready to create your first commit. What is a commit? Easy: A commit is a record of the files you’ve changed within the project. Creating the commit is actually quite simple. It is important, however, that you include a descriptive message for the commit. By doing this, you are adding notes about what the commit contains (such as what changes you’ve made to the file). Before we do this, however, we have to inform Git who we are. To do this, issue the command:</p>
<pre>
git config --global user.email EMAIL git config --global user.name “FULL NAME”</pre>
<p>Where EMAIL is your email address and FULL NAME is your name.</p>
<p>Now we can create the commit by issuing the command:</p>
<pre>
git commit -m “Descriptive Message”</pre>
<p>Where Descriptive Message is your message about the changes within the commit. For example, since this is the first commit for the readme.txt file, the commit could be:</p>
<pre>
git commit -m “First draft of readme.txt file”</pre>
<p>You should see output indicating that 1 file has changed and a new mode was created for readme.txt (Figure 4).</p>
<h3>Create a branch and push it to GitHub</h3>
<p>Branches are important, as they allow you to move between project states. Let’s say you want to create a new feature for your game-changing app. To do that, create a new branch. Once you’ve completed work on the feature you can merge this feature from the branch to the master branch. To create the new branch, issue the command:</p>
<p>git checkout -b BRANCH</p>
<p>where BRANCH is the name of the new branch. Once the command completes, issue the command <em>git branch</em> to see that it has been created (Figure 5).</p>
<p>Next we need to create a repository on GitHub. If you log into your GitHub account, click the New Repository button from your account main page. Fill out the necessary information and click Create repository (Figure 6).</p>
<p>After creating the repository, you will be presented with a URL to use for pushing our local repository. To do this, go back to the terminal window (still within<em> ~/newproject</em>) and issue the commands:</p>
<pre>
git remote add origin URL git push -u origin master</pre>
<p>Where URL is the url for our new GitHub repository. </p>
<p>You will be prompted for your GitHub username and password. Once you successfully authenticate, the project will be pushed to your GitHub repository and you’re ready to go.</p>
<h3>Pulling the project</h3>
<p>Say your collaborators make changes to the code on the GitHub project and have merged those changes. You will then need to pull the project files to your local machine, so the files you have on your system match those on the remote account. To do this, issue the command (from within <em>~/newproject</em>):</p>
<pre>
git pull origin master</pre>
<p>The above command will pull down any new or changed files to your local repository.</p>
<h3>The very basics</h3>
<p>And that is the very basics of using Git from the command line to work with a project stored on GitHub. There is quite a bit more to learn, so I highly recommend you issue the commands<em> </em><em>man git, man git-push</em><em>,</em> and <em>man git-pull</em> to get a more in-depth understanding of what the <em>git</em> command can do. </p>
<p>Happy developing! </p>
<p><em>Learn more about Linux through the free <a href="https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux">“Introduction to Linux” </a>course from The Linux Foundation and edX.</em></p>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016