Git tutorial 2 tips

Source: Internet
Author: User
Tags svn update git commands

Continue, article transferred from: http://www.jz123.cn/text/2129570.html

The previous article introduced the basic concepts and some basic commands of Git. This article focuses on the following three parts: personalized customization of your Git, cool and clever use of Git, and how to open your own open-source project on Git Hub. Among all the skills, the most important skill is to learn to view Git help, because Git is a relatively complex version control tool. If you are familiar with its commands, the value brought to you is self-evident, so you must learn to master the golden finger-view Git help, add '-- help' after any Git command to display the help document for this command. For example, 'git log -- help', you can see all the usage methods of the command 'git log. Next, start by dressing up Git.

Git makeup

We can use the Git config command or directly edit ~ /. Gitconfig file (if not, create it) to create a unique Git for yourself. We recommend that you directly edit the. gitconfig file in the user directory. Take my local file as an example to describe it one by one. The complete file content is as follows:

[User]

Name = Phoenix

Email = phoenixtoday@gmail.com

[Alias]

Co = checkout

Ci = commit-

St = status

Br = branch

Oneline = log -- pretty = oneline -- since = '2 days ago'

Onelog = log-p-1

[Color]

Status = auto

Branch = auto

Ui = auto

This file contains three parts:

User basic information: you can set your name and email so that your name will be displayed when you submit the code.

Command alias: This is my favorite part in The. gitconfig file. It can greatly reduce the number of times you hit the keyboard (as the saying goes, good programmers are very lazy ). In this file, I set co as the checkout alias. Next time I use 'git co new_branch ', I can switch to the new_branch branch, which is concise and elegant; set ci to the alias of commit-a. The-a option indicates that I do not need to add the modified and deleted files to the index using the 'git add' command, in this way, when the command 'git ci-m "message" 'is used, the 'git add modified and deleted files' and 'git
The two commit-m "message" 'commands save our precious time. the coolest thing is the last two lines, which will be described in the following sections. Git provides many elegant and user-friendly options. If we combine the alias settings, we can use your imagination to make your own Git live.

Color: Is it painful to watch diff every time? So why don't we add color to our Git? You only need to add the three lines to display the red and green prompts on your console.

Git agility

Now let's talk about the usage and skills of the 'git log', 'git stash ', and 'git formate-patch' commands:

Git log: Unlike SVN, Git clones all the code history records locally, which makes the command 'git log' very fast to use, it is also one of the most commonly used Git commands. You can add many suffixes when using 'git log. '-P' indicates the specific modification content. For example, 'git log-p' not only prints the submission time, version number, and personnel, the modified part of the code is printed. '-n' n indicates a number, which indicates several specific logs are printed, for example, 'git-p-1' is the same as the onelog alias set in my git configuration file. It indicates that the latest log record and specific modification content are printed; '-- since = "Time/date"', '-- until = "Time/date"' indicates that you want to query the log records of a specific date segment, such as 'git
Log -- since = "2 days ago" -- until = "1 hour ago" 'indicates that you want to find the log records from two days ago to one hour ago. Git is smart enough, it can convert the English letters that indicate time, such as '2 days ago 'and '1 hour ago', into specific time numbers. Sometimes, you don't want to go through many pages to view all the logs. You just want to see a brief description, so Git will provide you with custom print formats 'git -- pretty = format type ', the formats include full, short, and oneline. For example, 'git log pretty = oneline 'puts the history of each code in one row, which looks simple and clear.

Git stash: in the first article, I mentioned the problem of using branch to solve the urgent task switchover. In fact, the stash command can also solve this problem well. If you do not want to submit half of the Code that has been completed, but you have to modify an emergency Bug, you can use 'git stash 'to submit the code that has not been submitted locally (and on the server) the code is pushed into the Git stack. At this time, your work interval is exactly the same as the content submitted last time, so you can fix the Bug with confidence and wait until the Bug is fixed, after the application is submitted to the server, use 'git stash application' to return the previous half of the work. Some may say, Can I press uncommitted code into the Stack multiple times? The answer is yes. When you use 'git multiple times
After the stash command, your stack will be filled with unsubmitted Code. At this time, you will be confused about which version will be applied back, the 'git stash list' command can print the current git stack information. You only need to find the corresponding version number, for example, you can use 'git stash apply stash @ {1} 'to obtain the job with the specified version number stash @ {1, when you apply all the stacks back, you can use 'git stash clear' to clear the stacks.

Git format-patch: When you want to submit a piece of code for an open-source project (such as Rails), or you want to show the team members a piece of code that you do not want to submit, you still need patch. Git's 'format-patch 'command supports this function well. Let me briefly describe the steps and methods for using this command: first, use the branch command to create a branch; second, modify your code; and third, submit your modifications on the branch; fourth, run the 'git format-patch 'command to generate a patch file, for example, 'git format-patch master -- stdout
> ~ /Desktop/tmp. patch 'is the difference between the work Branch and the master, which is stored in '~ In the/Desktop 'folder, generate a file named tmp. patch (another simple version is to use the diff command, such as 'git diff.. master> ~ /Desktop/tmp. patch ') to generate the patch file. Then someone else can use the 'git apply 'command to apply the patch, for example, 'git apply ~ /Desktop/tmp. patch: load the patch to the current work branch.

Git friends

Git usage tips include using Git-included and attached powerful tools, including git svn, git citool, gitk, and Git automatic prompt scripts:

Git svn: Git and SVN can be easily integrated, which greatly reduces the learning cost for migration from SVN to Git, this is the first time that I suggest you use Git. Git svn is a built-in Git tool. After you install Git, you can install it. For example, your team has a SVN server, but you want to use some of the powerful features of Git locally, you can still install Git and use the branch Function of Git, but use the git svn command when updating the Code and submitting the code. Here I will give a brief description of the two most commonly used commands and the two commands that need attention. Other command readers can view them through 'git svn -- help': 'git
The svn rebase command replaces 'svn Update' to update the server code locally. The 'git svn dcommit 'command replaces 'svn ci'. Note that, after the Code must be submitted using Git locally, use 'git svn dcommit '. You can easily switch from SVN to Git.

Git citool: This is my most frequently used tool. As mentioned in the previous article, Git can submit code locally, so you can modify your submission locally, this tool is a visual interface used to modify your local submission. If you enter 'git citool 'in your work interval, the following interface will appear:

  


 

You can use it to submit code and append your local modifications to the Code submitted last time. You can also use it to modify the information you submitted last time. This tool can greatly help you complete tasks that were previously impossible for SVN.

Gitk: a tool used to view the status of the trunk or branch. It is mainly used to observe the status of the branches of the entire project. You can view the status of the branches using the 'gitk' command, in this article, we will give you a simple explanation.

Git's automatic prompt Script: It is Shawn O. pearce writes Shell scripts to make it easier for Git to use, you can find a script called gitcompletion in the http://gitweb.hawaga.org.uk/, download it, and configure it as instructed in the script, you have the Git automatic prompt function (press the Tab key after you press some Git commands), and with this script, you can also see the branch under which you are currently working. The only disadvantage is that it only supports Linux, Unix, and Mac operating systems (we recommend that you use Mac for development)

Git practice-Git Hub

After a long time of theoretical knowledge, I think everyone is already eager to try it out. Let's create an open-source project on Git Hub as a practical exercise. Git Hub is the world's largest Git server supplier, each account has m free use space, web site is: https://github.com/

First, create an account on Git Hub and set your authentication information as instructed above (this authentication information is required for every code submission)

Then, for example, Create a New Repository project named git usage

  


 

On the server side, you can see the project information, including the project source code URL, such

  


 

Use the following command locally to complete your first submission:

Mkdir git-usage (Create project directory) cd git-usage (enter the project directory) git init (Git initialization) touch README (create a README file) git add README (add this file to the index) git commit-m 'first commit '(local commit) git remote add origin git@github.com: phoenixtoday/git-usage.git (add remote server code library address) git push origin master (submit local code to remote server)

Summary

Since using git, my project team has found that the features provided by git have greatly improved our development efficiency, before learning about git, we could not imagine a version control tool that could make the development task switching so natural and smooth. Therefore, I strongly recommend that you use git. Your learning is definitely worth the money.

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.