Dịch vụ lưu trữ Linux (cPanel) Trợ giúp

Git tutorial and basics

Git is a free and open source distributed version control (DVC) system for software development. The core features of Git have contributed to it quickly becoming one of the most popular and powerful DVC systems in use today, particularly among open source communities.

DIFFICULTY Basic - 1 | Medium - 2 | Advanced - 3
TIME REQUIRED 15 min
RELATED PRODUCTS Linux-based VPS or dedicated servers
Linux-based shared hosting

Git is a version control system that enables developers to better manage their repositories of code during web development projects. Git is especially adept at branching, staging and distribution of code, as well as ensuring the integrity of a code base.

Branching

One of the most powerful and somewhat unique features Git provides is the ability to work with multiple, completely separate branches of your code without affecting other branches. Development can occur within a unique branch outside of the master branch and be later reintegrated.

Staging

Another fairly unique feature to Git is the intermediary "staging" area where files (or even chunks of code) can be stored before commitment into the repository. This enables multiple edits within the same file to be staged (and therefore committed) on separate occasions if necessary, without the need to commit the entire file after every change is made.

Distribution

Although not unique to Git, as a distributed version control system Git allows virtually any workflow that may best serve you or your team or clients. Git has no built-in requirement for central repositories, making it particularly suited to open source projects and other workflows that benefit from a widely spread developer network.

Integrity

Every object that passes through a Git repository passes through a checksum verification process, both when files are entered in or taken out. This ensures that no part of Git repository can be altered without changing the commit ID, providing assurance that a commit always remains identical to when it was entered.

Let's get started.

Installing Git

Git releases are available for all major operating systems and while Git is typically utilized from the command line, there are numerous GUI clients available across a variety of platforms.

Windows and Mac installation applications are available from the official Git downloads page, while installation on popular Linux system can be performed with a single command:

Installation on Ubuntu/Debian

$ apt-get install git

Installation on Fedora/CentOS

$ yum install git

Getting Started

While the detailed information can be found within the official Git documentation, getting started with your first Git repository is very simple and only requires a few minutes to begin.

Once Git is installed, begin by navigating into an empty directory that will be your root directory for your project. For this example our project will live in the ~/dev/foo directory:

root@Main:~/dev/foo# mkdir ~/dev/foo
root@Main:~/dev/foo# cd ~/dev/foo

Initializing Git

Once inside your project's root directory, enter git init to generate a new Git repository:

root@Main:~/dev/foo# git init

Which returns:

Initialized empty Git repository in /root/dev/foo/.git/

Adding Changes

Once you've added a file to your project directory, you need to tell Git which files should be entered into the repository and tracked by Git. Here we're adding a data.xml file to the project directory, then using the git add [file] command to tell Git that we want data.xml in the system:

root@Main:~/dev/foo# touch data.xml
root@Main:~/dev/foo# git add data.xml

Status

One of the most common tools in Git is the git status command, which provides information on the overall commitment status of your project. In this case after adding our data.xml file to Git, we see that the file is waiting to be committed:

root@Main:~/dev/foo# git status

Which returns:

On branch master
Initial commit
Changes to be committed:
  (use "git rm --cached ..." to unstage)
        new file:   data.xml

Committing

After adding files to the repository the next step is to create a commit, which can be performed easily with the git commit command:

root@Main:~/dev/foo# git commit

Note: If this is your first time using Git on this machine you may encounter a warning upon attempting to commit regarding setting your git config user.email and user.name values. If necessary, follow the instructions on screen to set these and retry the commit command.

Once the git commit command is issued, you will typically be presented with a text editor screen in which to edit the commit message. Enter an appropriate message and save to finalize your commit:

Added the data.xml file.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
#
# Initial commit
#
# Changes to be committed:
#       new file:   data.xml
#
[master (root-commit) ce19fd5] Added the data.xml file.
 1 file changed, 1 insertion(+)
 create mode 100644 data.xml

Alternatively, you can add the -m flag to your git commit command, which allows you to enter a commit message directly in-line rather than through the additional commit message file:

root@Main:~/dev/foo# git commit -m "Added the data.xml file."

Which returns:

[master (root-commit) ce19fd5] Added the data.xml file.
 1 file changed, 1 insertion(+)
 create mode 100644 data.xml

While this tutorial should give you a solid grasp on what Git is and how you can potentially use it to improve your version control workflow, we've just scratched the surface of what Git can do and how you can take full advantage of its capabilities.

To learn even more about Git, check out the full documentation here, look through the command reference manual, or even try out the basics of Git in your browser!


Bài này có hữu ích không?
Thanks for your feedback. To speak with a customer service representative, please use the support phone number or chat option above.
Rất vui vì chúng tôi đã giúp được bạn! Chúng tôi có thể làm gì thêm cho bạn?
Rất tiếc về điều đó. Tell us what was confusing or why the solution didn’t solve your problem.