TranslateProject/sources/tech/20180720 An Introduction to Using Git.md
2018-07-26 16:01:55 +08:00

8.8 KiB
Raw Blame History

translating by distant1219

An Introduction to Using Git

If youre a developer, then you know your way around development tools. Youve 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.

But what happens when youre tasked with collaborating on a project? Or what about when that app youve developed becomes bigger than just you? Whats the next step? If you want to successfully collaborate with other developers, youll 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 Git. Along with Git comes a handy repository called GitHub, where you can house your projects, such that a team can check out and check in code.

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. Ill be demonstrating on Ubuntu 18.04, so if your distribution of choice is different, youll only need to modify the Git install commands to suit your distributions package manager.

Git and GitHub

The first thing to do is create a free GitHub account. Head over to the GitHub signup page and fill out the necessary information. Once youve done that, youre ready to move on to installing Git (you can actually do these two steps in any order).

Installing Git is simple. Open up a terminal window and issue the command:

sudo apt install git-all

This will include a rather large number of dependencies, but youll wind up with everything you need to work with Git and GitHub.

On a side note: I use Git quite a bit to download source for application installation. There are times when a piece of software isnt available via the built-in package manager. Instead of downloading the source files from a third-party location, Ill often go the projects Git page and clone the package like so:

git clone ADDRESS

Where ADDRESS is the URL given on the softwares Git page. Doing this most always ensures I am installing the latest release of a package.

Create a local repository and add a file

The next step is to create a local repository on your system (well call it newproject and house it in ~/). Open up a terminal window and issue the commands:

cd ~/

mkdir newproject

cd newproject

Now we must initialize the repository. In the ~/newproject folder, issue the command git init. When the command completes, you should see that the empty Git repository has been created (Figure 1).

new repository

Figure 1: Our new repository has been initialized.

Used with permission

Next we need to add a file to the project. From within the root folder (~/newproject) issue the command:

touch readme.txt

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).

readme

Figure 2: Git knows about our readme.txt file.

Used with permission

Even though Git is aware of the file, it hasnt actually been added to the project. To do that, issue the command:

git add readme.txt

Once youve done that, issue the git status command again to see that readme.txt is now considered a new file in the project (Figure 3).

file added

Figure 3: Our file now has now been added to the staging environment.

Used with permission

Your first commit

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 youve 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 youve made to the file). Before we do this, however, we have to inform Git who we are. To do this, issue the command:

git config --global user.email EMAIL

git config --global user.name “FULL NAME”

Where EMAIL is your email address and FULL NAME is your name.

Now we can create the commit by issuing the command:

git commit -m “Descriptive Message”

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:

git commit -m “First draft of readme.txt file”

You should see output indicating that 1 file has changed and a new mode was created for readme.txt (Figure 4).

success

Figure 4: Our commit was successful.

Used with permission

Create a branch and push it to GitHub

Branches are important, as they allow you to move between project states. Lets say you want to create a new feature for your game-changing app. To do that, create a new branch. Once youve 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:

git checkout -b BRANCH

where BRANCH is the name of the new branch. Once the command completes, issue the command git branch to see that it has been created (Figure 5).

featureX

Figure 5: Our new branch, called featureX.

Used with permission

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).

new repository

Figure 6: Creating the new repository on GitHub.

Used with permission

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 ~/newproject) and issue the commands:

git remote add origin URL

git push -u origin master

Where URL is the url for our new GitHub repository.

You will be prompted for your GitHub username and password. Once you successfully authenticate, the project will be pushed to your GitHub repository and youre ready to go.

Pulling the project

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 ~/newproject):

git pull origin master

The above command will pull down any new or changed files to your local repository.

The very basics

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 man git, man git-push, and man git-pull to get a more in-depth understanding of what the git command can do.

Happy developing!

Learn more about Linux through the free "Introduction to Linux" course from The Linux Foundation and edX.


via: https://www.linux.com/learn/intro-to-linux/2018/7/introduction-using-git

作者:Jack Wallen 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出