Home >System Tutorial >LINUX >Git Series (2): Getting Started Guide to Git
Introduction | Because Git is so popular, if you can at least be familiar with some basic Git knowledge, it can bring a lot of convenience to your life. If you can master the basics of Git (you can, I swear!), then you'll be able to download anything you need and maybe even make some contributions in return. After all, that's the essence of open source: you have access to the code of the software you use, the freedom to share it with others, and the right to modify it if you wish. Git makes this easy once you get familiar with it. |
Generally speaking, there are two ways to interact with a Git repository: you can read from the repository, or you can write to the repository. It's like a file: sometimes you open a document just to read it, and other times you open it because you need to make some changes.
This article only explains how to read from the Git repository. We will cover the topic of writing back to a Git repository in a later article.
One sentence to clarify: Git is different from GitHub (or GitLab, or Bitbucket). Git is a command line program, so it looks like this:
Because Git is open source, many smart people have built basic software around it; these basic software, including around themselves, have become very popular.
My article series will start by teaching you pure Git knowledge, because once you understand what Git is doing, then you don't need to care about what front-end tool you are using. However, my article series will also cover common ways to accomplish each task through popular Git services, since those will likely be the first ones you'll encounter.
On Linux systems, you can obtain and install Git from the distribution software repository you are using. BSD users should look for Git in the devel section of the Ports tree.
For closed source operating systems, please go to its official project website and install according to the instructions. Once installed, there should be no difference in the commands on Linux, BSD, and Mac OS X. Windows users will need to adjust Git commands to match Windows file systems, or install Cygwin to run Git natively without being hampered by Windows file system translation issues.
Not everyone needs to add Git to our daily life right away. Sometimes, your most interaction with Git is to access a repository, download a file or two, and then never use it again. Think of Git this way, it's more like afternoon tea than a formal banquet. You have some polite conversation, get the information you need, and then you go away and you don't want to talk like this anymore for at least the next three months.
Of course, that's okay.
Generally speaking, there are two ways to access Git: using the command line, or using one of those amazing Internet technologies for quick and easy access through a web browser.
Let's say you want to install a recycle bin for your terminal because you've been destroyed by the rm command too many times. You may have heard of Trashy, which calls itself a "sane rm command middleman", and maybe you want to read its documentation before installing it. Luckily, Trashy is hosted publicly on GitLab.com.
The first step in our work is to use the landgrab sorting method on this Git repository: we will clone the complete repository and then sort according to the content. Since the repository is hosted on a public Git service platform, there are two ways to get the job done: using the command line, or using the web interface.
To get the entire repository using Git, use the git clone command and the URL of the Git repository as parameters. If you don't know what the correct URL is, the repository should tell you. GitLab provides you with a copy-and-paste URL to the Trashy repository.
Once you have successfully cloned the repository, you can browse the files in the repository just like any other directory on your computer.
Another way to obtain a copy of the repository is to use the web interface. Both GitLab and GitHub provide a warehouse snapshot file in .zip format. GitHub has a big green download button, but in GitLab, you can find the inconspicuous download button on the right side of the browser.
Another way to get files from a Git repository is to find the file you want and then pull it out of the repository. Only the web interface provides this method, and essentially, what you see is a clone of someone else's repository; you can think of it as an HTTP shared directory.
The problem with using this method is that you may find that some files do not exist in the original repository, because the complete form of the file may not be built until you execute the make command. That is only if you download the complete repository and read the README. Or INSTALL file, and then run the relevant commands to generate it. However, if you're sure the file exists and you just want to go into the repository, get that file, and then leave, you can do that.
In GitLab and GitHub, click the file link and view it in Raw mode, then use your web browser's save function, for example: in Firefox, File > Save Page As. In a GitWeb repository (which is a private git repository web viewer used by some people who prefer to host git themselves), the Raw view link is in the file list view.
It is generally believed that the correct way to interact with Git is to clone the complete Git repository. There are several reasons for thinking this way. First, you can easily keep your cloned repository updated using the git pull command, so you don't have to go back to the web site to get a fresh copy every time a file changes. Second, if you happen to need to make some improvements, as long as you keep the repository clean, you can submit the changes to the original author very easily.
Now, it might be time to practice finding Git repositories of interest and then cloning them to your hard drive. As long as you know the basics of using the terminal, it's not too difficult to do. Still don’t know how to use the basic terminal? Then give me 5 more minutes.
The first thing to know is that all files have a path. This makes sense; if I ask you to open a file for me in a regular non-terminal environment, you have to navigate to the file's location on your hard drive, and until you find that file, you have to navigate through a bunch of windows. For example, you might click on your Home Directory > Images > InktoberSketches > monkey.kra.
In that case, the path to the file monkeysketch.kra is: $HOME/Pictures/InktoberSketches/monkey.kra.
In the terminal, unless you're doing some special sysadmin tasks, your file paths usually start with $HOME (or, if you're lazy, use the ~ character), followed by a list of folders up to the filename itself.
This is similar to how you click on various icons in a GUI until you find the relevant file or folder.
If you want to clone the Git repository to your documents directory, then you can open a terminal and run the following command:
Once the cloning is complete, you can open a file manager window, navigate to your Documents folder, and you will find the bar.clone directory waiting for you to access.
If you want to get more advanced, you may want to visit that repository again in the future, and you can try using the git pull command to see if the project has been updated:
So far, that’s all the terminal commands you need to know initially, so go explore. The more you practice, the better you get at Git (practice makes perfect), that's the point and the nature of the matter.
The above is the detailed content of Git Series (2): Getting Started Guide to Git. For more information, please follow other related articles on the PHP Chinese website!