8 tips for Git

Source: Internet
Author: User

8 tips for Git

Git has become one of my daily necessities. I sum up the eight useful (and concise) tips I use every day.

Use-p for selective Addition

When you want to submit content, you can use git commit-am to select all files or use git add file to add specific files. However, sometimes you may want to add only a part of the file for submission. You can use git add-p to select the parts you want to submit interactively.

After selecting the block you want to submit, you only need to make a git commit (no-a). This will only submit the selected part. You can also use git checkout-p to select the part to be restored. After adding the SDK, you can use git diff -- cached to view the differences.

Interactive Rebase)

If you work on a branch and perform some WIP commit (commit) at the same time to compress the Merge (squash) or delete a commit and restore the commit, you can create an interactive rebase to reorganize the submission.

To do this, you need to run the git rebase-I <commit> command. The <commit> here is the sha1 value of the previous commit (commit) You want to rewrite. Next, it will open some commands in your EDITOR (in the $ EDITOR Environment Variable or the EDITOR specified in the git configuration) to change the commit history, you can choose to compress and merge (merge two commits into one new commit), rewrite (Change submission information), edit or even delete a commit (commit ).

Please note that this has changed the historical information. Therefore, if you submit this change, you will have to force commit again (push). Therefore, do not go to the main branch, or someone else (except you) will perform this operation on the used Branch.

Storage (Stashing)

If you are busy with something, you must change the text to fix other problems and go to git stash to find the changes that are stored in the current state. However, after a while, you may forget the stored changes. Therefore, I tried to keep a storage 0 (just like inbox 0 if there is no storage) rule. Every time I store a dollar signal in my output, and I check it through git stash show-p, it can also be popped up through git stash pop or discarded through git stash clear.

Global gitignore

In the project root directory, you can use file. gitignore to specify the files to be ignored by git. However, if there are files that need to be ignored by git, and you are the only one who creates this file (such as the vim bkp file, the editor or some files generated by the operating system, such as the one generated by OSX. DS_Store file), you can specify the global gitignore file in the configuration file, it and the project. the gitignore file uses the same syntax format.

git config --global core.excludesfile=/Users/flores/.gitignore
Space warning

I have to admit: Sometimes I forget some spaces at the end of the Code. However, I usually do not submit them because I use this option: apply. whitespace = warn. Each time I add a large piece of code in the file through git add-p, and the code block contains spaces at the end of the code block, I can correct them before submission because git gives a warning.

Auto setup rebase)

Another cool technique is auto setup rebase ). If you have a branch with some commit but not push. At the same time, others also performed commit and push on this branch. When you use pull, git will create a commit to merge your commit to the upstream (upstream) commit. Since this commit is meaningless, I prefer to automatically rebuild the code benchmark (auto setup rebase): branch. autosetuprebase = always in pull. After setting, git tries to re-use your commit in the current version of the upstream (upsteam) Branch for each pull operation.

Better logging)

Have you ever tried to find a specific commit in a branch? Git log provides some basic information, but you can use the following command to obtain more useful information:

Git log -- graph -- decorate -- pretty = oneline -- abbrev-commit

-- Graph prints a line entry between each submission. These lines can show the relationship between the branches.

-- Decorate indicates the submission of the branch.

-- Pretty = oneline only displays the sha1 and comments submitted in a row (the translator uses the title term as a more accurate comment)

-- Abbrev-commit replaces the entire sha1 with the first seven sha1 characters (which is unique in your repository ).

You can go to the explain shell to see more detailed (and complete) explanations of these options.

Rewrite comments submitted

If your comments cannot accurately describe the current submission when submitting code, or you have written an error. You can use git commit -- amend to rewrite the comments you have submitted. It allows you to specify a new comment through the-m option in the command line or directly open the System Default Editor for you to edit the new comment. In addition, you can add some new changes to the last commit. Remember that this operation is the same as Interactive Rebase, and it will change the submission history. If you have submitted and pushed your change, you need to force the change.

You are welcome to share in your comments some tips on git that can improve your work efficiency.

GitHub Tutorials:

GitHub tutorials

Git tag management details

Git branch management

Git remote repository details

Git local Repository (Repository) Details

Git server setup and Client installation

Git Overview

Share practical GitHub tutorials

Git details: click here
Git: click here

8. Small git tips

This article permanently updates the link address:

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.