10 minutes to learn about the local use of Git under the Linux system

Source: Internet
Author: User
Tags aliases using git version control system

Around the technology Daniel has always advised me to use Git for code management, and I have forgotten this thing every time after the brain. Recently, it seems that the importance of git is felt. Here's the simplest way to get started with git, just for rookie-level git players like Xun month .

Git is a distributed version control software that was originally designed by the Linux kernel developer Linus Torvalds to better manage the development of the Linux kernel.

Key Features

Git is a version control tool for Linux kernel development. Unlike the centralized version control tools of CVS and subversion, it uses a distributed repository approach that does not require server-side software to operate version control, making it easy to publish and communicate source code. Git is fast, which is naturally important for large projects such as the Linux kernel. The best of Git is its combined tracking (merge tracing) capability.

In fact, when the kernel development team decided to start developing and using Git as a kernel-developed version control system, the world's open source community had a lot of objections, and the biggest reason was that git was too difficult to understand, and from the internal working mechanism of git, it did. But with the development in-depth, Git's normal use is done by some friendly command drafts, making git very useful.

Work steps

1. Install git

Xun Month is an Ubuntu system that can be installed using Apt-get install Git.

2. Set up Git

Before we can work with git, we need a one-time configuration. In order for git to be able to track who made the changes, we need to set up your username.

git config--global user.name "Bear2flymoon"

git config--global user.email [email protected]

Here my user name is set to: Bear2flymoon

Email: [Email protected] (Google is wall, unable to login)

3. Create a local code base

Suppose our code is written in the ' Test ' folder under the/home/bear2flymoon folder of the machine. Enter the following command at the command line:

cd/home/bear2flymoon/test/

Below, we need to tell Git that this folder is the item we need to track. So let's initialize a new local git code base by entering the following command in this directory

Git init

Git creates a hidden folder named. Git under the Test folder, which is your local code base.

4. Staging (stage) file

We need all the project files in the staging (stage) test folder and enter the following command:

git Add.

The last "." The symbol means "All files, folders, and subfolders". If we only need to add specific files to source code control, we can specify them:

git add my_file,my_other_file

5. Submission of documents

Now we want to submit a file that has already been staged (staged). When we submit our documents, we always attach meaningful annotations that describe their present state. I've been using "initial commit" to describe my first commit comment.

git commit-m "initial commit"

Again, you can roll back to this commit state at any time. If you need to check the status, commit, etc. of files that are now staged and not staged, you can enter the following command to ask for GIT status:

git status

6. Create a branch

Creating a branch is the act of creating a separate version of your code that is independent of your backbone. By default, every code you commit to git will be stored in the "Master (Trunk)" Branch.

Now, for example, we want to add a feature to the project, but we want to be able to roll back to the current version, to prevent errors, or we will eventually abandon this feature. At this point, we can create the branch and switch to the new branch and send the command:

Git checkout-b new_feature

or create a branch first, and then switch

Git branch new_feature

git checkout new_feature

To see all the branches under the project, send the command:

Git branch

Now you can do whatever you want to do on your project: At any time, you can go back to the state you were in before you created the branch. Note that you can have multiple branches at the same time, and you can even create a branch from a branch.

7. Merging branches

When we are satisfied with the new feature, we want to add it to the trunk branch. When we are on the new feature branch, we first need to load and submit the file:

git Add.

Git commit-m "adds my new feature"

Then move to the trunk branch:

git checkout Master

Merge like this:

git merge New_feature

At this point, our Trunk branch and the new function Branch will become the same.

8. Discard and Delete branches

When we want to discard and delete a branch, we first load our file and commit it in the branch:

git Add.

Git commit-m "feature to be discarded"

Then, move to the trunk branch

git checkout Master

Then send the Delete command

Git branch-d new_featrue

If the changes have been merged, it will only delete the branch. If the branch is not merged, we get an error message. Delete an unincorporated branch (usually we don't want to keep the modifications), we need to send the same command with an uppercase d. It means "force Delete branch, anyway I don't want it anymore." ”:

Git branch-d new_feature

9. Rollback to a previously committed state

In some cases, we might want to go back to the previous version of the Code. First, we need to find out which version we want to go back to. To view all completed commits, send the command:

git log

History of output submissions

Commit ca82a6dff817ec66f44342007202690a93763949author:your_username [email protected]: Mon-Nov4  A: the: One  --0700changes the FrontPage layoutcommit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7author:your_username [email protected ]: Mon-Nov4  One: +: -  --0700Adds myNewfeaturecommit a11bef06a3f659402fe7563abf99ad00de2209e6author:your_username [email protected]: Mon-Nov4 Ten:Panax Notoginseng: -  --0700Initial commit

If we want to go back to "adds my new feature" This commit, simply use the submitted ID to do checkout (checkout) (usually only with the first 9 letters of the ID)

git checkout 085BB3BCB

We can also check out a new branch

git checkout-b my_previous_version 085BB3BCB

Just don't be so crazy! The more complex the branching, the harder it is for us to determine what we are doing.

10. Aliases

Git allows us to create shortcuts (aliases) for commonly used commands. For example, if we don't want to enter Git commit-m "some comment" every time, but instead enter git C "some comment", we can add an alias to the GIT global configuration, like this:

git config--global alias.c ' commit-m '
Here is the list of aliases I used:
git config--global alias.c ' commit-m '
git config--global alias.co ' checkout '
git config--global alias.cob ' checkout-b '
git config--global alias.br ' branch '
git config--global alias.m ' merge '
git config--global alias.a ' Add. '
git config--global alias.s ' status '
git config--global alias.dbr ' branch-d '

This allows you to use Git for the most basic version management of the program!

Learning materials:

Pro Git http://iissnan.com/progit/

This document is compiled from the following information:

Wikipedia Http://zh.wikipedia.org/zh-cn/Git

Bole Online http://blog.jobbole.com/53573/

Special thanks

2015-03-14 Xun Month

10 minutes to learn about the local use of Git under the Linux system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.