8 Tips for Git

Source: Internet
Author: User
Tags sha1

Using-p to selectively add

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 might want to just add a portion of the file to submit. You can use Git add-p to interactively select the parts that you want to commit.

After selecting the chunk you want to commit, you only need to make a git commit (no-a) so that only the selected part is committed. You can also use Git checkout-p to select the parts that need to be recovered. Once added, you can use Git diff–cached to see the differences.

Interactive Rebuild Benchmark (Interactive Rebase)

If you are working on a branch and have done some WIP commits (commit) to compress the merge (squash) or delete a commit (commit) and the recovery of this commit, you can do an interactive rebuild baseline (rebase) to reorganize the commit.

To do this, you need to run the command git rebase-i <commit>, where <commit> is the SHA1 value you want to rewrite before committing a commit. Next, it will open some commands on 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 the merge (merge two commits into a new commit), rewrite (change commit information), Edit or even delete a commit (commit).

Note that this changes the history information, so if you commit the change, you will have to force the commit again, so never do this on the main branch, or on the branch that someone else (except you) is using.

Storage (stashing)

If you are busy with something, you have to change the text to fix other problems, go to git stash to find the changes stored in the current. However, after a while, you may forget about this stored change. So, I'm trying to keep a storage 0 (like Inbox 0 if there's no storage) rule. Every time I store a dollar signal appearing on my output, and I check through git stash show-p, I can also pop it through git stash or discard it through git stash clear.

Global Gitignore

At the root of the project, you can specify files that git needs to ignore by using the file. Gitignore. However, if there are files that Git needs to ignore, and you are the only person who created the file (such as Vim's BKP file, editor, or some files generated by the operating system, such as the. ds_store file generated by OSX), you can specify the global gitignore file in the configuration file. It uses the same syntax format as the. gitignore file in the project.

git config--global core.excludesfile=/users/flores/.gitignore

Space warning

I have to admit: sometimes I forget some of the blanks in the tail of the code. However, I usually don't commit them because I use this option: Apply.whitespace=warn. Every time I add a chunk of code to a file via Git add-p, the end of the block contains spaces, and because git gives a warning, I can fix them before committing.

Auto-rebuild code base (Auto setup rebase)

Another cool trick is to automatically rebuild the code base (Auto Setup rebase). If you have a branch and did some commit but did not push. At the same time, other people have made a commit and push on this branch. When you pull, Git creates a commit to merge your commit to the upstream (upstream) commit. Since this commit is meaningless, I prefer to automatically rebuild the code base (auto Setup rebase) by configuration at pull time: branch.autosetuprebase=always. Once set, Git tries to re-use your commit on the upstream (upsteam) branch of the current version for each pull operation.

Better logs (logging)

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

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

–graph will print lines between submissions that show the relationship between the branches.

–decorate shows which commits the branch is on.

–pretty=oneline only displays SHA1 and submitted comments in a row (the translator will address the title to a more precise note)

–abbrev-commit replaces the entire SHA1 with the beginning of the 7 SHA1 characters (he is unique in your warehouse).

You can go to the explain Shell to see a more detailed (and complete) explanation of these options.

Overwrite a submitted comment

If you submit the code, the comment does not accurately describe the current submission, or you write a typo. You can use Git commit–amend to rewrite comments that you've already submitted.  He allows you to specify a new comment by using the-M option on the command line or directly open the system default editor so you can edit the new comment. In addition, you can add some new changes to the last commit. Keep in mind that this action, like Interactive Rebase, will change the commit history. If you have already made your changes to this submission, you will need to force the change to push.

8 Tips for Git

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.