10 tips for developers to quickly improve their Git level (1)

Source: Internet
Author: User
Tags how to use git git commands

10 tips for developers to quickly improve their Git level (1)

Recently we have released two Tutorials: getting familiar with the basic features of Git and letting you use Git skillfully in the development team. The commands we have discussed are enough for a developer to use Git. In this article, we try to find out how to effectively manage your time and fully use the features provided by Git.

Note: In this article, some commands contain part of square brackets, for example:Git add-p [file_name]In these examples, you will insert necessary numbers, identifiers, and so on, if there is no square brackets.

1. Git auto-completion

If you use the command line tool to run Git commands, it is annoying to manually enter various commands each time.

To solve this problem, you can enable the Git auto-completion function. It takes only a few minutes to complete this task.

To get this script, run the following command on Unix:

 
 
  1. cd ~  
  2. curl https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash 

Then, add the following lines to your ~ /. Bash_profile file:

 
 
  1. if [ -f ~/.git-completion.bash ]; then  
  2.     . ~/.git-completion.bash  
  3. fi 

Although we mentioned this earlier, the emphasis is not sufficient. If you want to use all the features of git,

You should definitely switch to the command line interface!

2. Ignore files in Git

Are you bored with those compiled files (such as. pyc) appearing in your Git repository? Or have you gotten enough of them all added to the Git repository? Well, there is a way for you to tell Git to ignore those specific files and folders. You only need to create a file named. gitignore and list the files and folders that you do not want Git to trace. You can also add exceptions by using an exclamation point (!).

 
 
  1. *.pyc  
  2. *.exe  
  3. my_db_config/  
  4.    
  5. !main.pyc 

3. Who messed up my code?

When something goes wrong, it is human nature to blame others first. If your product server crashes, you can easily find out the culprit by using the git blame command. This command displays the author of each row in the file, the latest change submission time, and the submission time.

 
 
  1. git blame [file_name] 

In the following example, you can see how the command is searched in a larger directory.

4. View Warehouse history

We have learned how to use git log in the previous section. However, there are three options you should know.

  • -- Oneline-compression mode. The submitted hash code and submission information are displayed next to each submission in one row.

  • -- Graph-graph mode. This option is used to draw a historical graph based on text format on the left of the output. This option is invalid if you are viewing the history of a single branch.

  • -- All-display historical records of all branches

After combining these options, the output will look like this:

5. Never lose tracking of Commit

If you accidentally submit something you don't want, you have to perform a force reset to restore it to the previous state. Then, you realize that you have lost some other information and want to get them back, or at least glance. This is exactly what git reflog can do.

A simple git log command can show you the last commit, its father, its father, and so on. While git reflog lists a series of commit that head once pointed. You must understand that they exist only in your local machine, not part of your version repository, and are not included in the push and merge operations.

If I run the git log command, I can see some commit, which are part of my Repository:

 

However, a git reflog command shows a commit (b1b0ee9 & #8211; HEAD @ {4}), which is exactly what I lost during a force Reset:

 


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.