25 Git usage tips

Source: Internet
Author: User

25 Git usage tips

Andy Jeffries summarized 25 tips for Git intermediate users. You don't need to do a lot of searching. Maybe these tips are very helpful to you.

I have been using git for almost 18 months, and I think I understand git very well. It wasn't until I saw Scott Chacon teaching on LVS, a supplier/developer of betting/gaming software on github that I had benefited a lot on the first day.

As a git-loving person, I want to share my practical experience from various communities so that you can find the answer without spending too much effort.

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

Basic Skills 1. Step 1 after installation

After installing git, you need to set your name and email address first, because this information is required for each commit.

$ git config --global user.name "Some One"$ git config --global user.email "someone@gmail.com"
2. pointer-based

Everything on git is stored in files. When you create a commit, it creates a commit that contains your information and related data (name, email address, date/time, last commit, etc.) and connect to a tree file, which contains the object list or other trees. The above object or blob FILE is the actual content submitted this time (you can think that this is also a file, although it is not stored in the object but stored in the tree ). All files are stored in the file name after SHA-1 calculation (note: the number of files after SHA-1 calculation, that is, the version number in git.

It can be seen from this that both the branch and tag contain a simple file pointing to the number of sha-1 (Version Number) submitted this time, so that the use of reference will become faster and more flexible, creating a new branch is as simple as creating a file. SHA-1 (Version Number) also references the submission of this branch. Of course, if you use the GIT command line tool (or GUI), you will not be able to access this. But it's really easy.

You may have heard of HEAD references. This is a pointer to the SHA-1 number (version number) of the content you submit. If you are resolving a merge conflict, using HEAD will not change your specific branch but will only point to your current branch.

The pointers of all branches are saved in. git/refs/heads, the HEAD pointer is saved in. git/HEAD, and the labels are saved in. git/refs/tags. If you have time, let's take a look.

3. Two Parent bodies (Parent), of course!

When we view the merge submission information in the log file, you will see two parent trees. The first parent is the branch in progress, and the second is the branch you want to merge.

4. Merge conflicts

Now, I have found that there are merge conflicts and solved them. This is normal when we edit files. Remove the <, ====, >>> tags and save the code you want to save. Sometimes, before the code is directly replaced, it is quite good to see the conflict. For example, before two conflicting branch changes, you can use the following command:

$ git diff --mergediff --cc dummy.rb  index 5175dde,0c65895..4a00477  --- a/dummy.rb+++ b/dummy.rb@@@ -1,5 -1,5 +1,5 @@@  class MyFoo    def say-     puts "Bonjour" -    puts "Hello world"++    puts "Annyong Haseyo"    end  endIf the file is binary, diffing files isn’t so easy… What you’ll normally want to do is to try each version of the binary file and decide which one to use (or manually copy portions over in the binary file’s editor). To pull a copy of the file from a particular branch (say you’re merging master and feature132):

If it is a binary file, it is not easy to distinguish these files. Generally, you can view the version of each binary file, decide which version to use (or manually copy it in the binary file editor), and push it to a specific branch. (For example, you want to merge master and feature132)

$ git checkout master flash/foo.fla # or...$ git checkout feature132 flash/foo.fla$ # Then...$ git add flash/foo.flaAnother way is to cat the file from git – you can do this to another filename then copy the correct file over (when you’ve decided which it is) to the normal filename:

Another method is to cat the file in git. you can name it as another file name and change the file you decide to the correct File Name:

$ git show master:flash/foo.fla > master-foo.fla$ git show feature132:flash/foo.fla > feature132-foo.fla$ # Check out master-foo.fla and feature132-foo.fla$ # Let's say we decide that feature132's is correct$ rm flash/foo.fla$ mv feature132-foo.fla flash/foo.fla$ rm master-foo.fla$ git add flash/foo.fla

Update: Thanks to carls for reminding me in the original blog comment that you can use "git checkout-ours flash/foo. fla and git checkout-theirs flash/foo. fla "does not need to consider the branches you need to merge to check the specified version. Personally, I prefer a clearer method, but this is also an option...

Remember to add files after resolving the merge conflicts. (I made this mistake before)

Services, branches, and labels 5. remote services

Git has a very powerful feature, that is, it can have multiple remote servers (and a local repository that you run ). You do not need to always access the service. You can have multiple servers and read data from one server (merging) before writing data to another server. It is easy to add a remote server:

$ git remote add john git@github.com:johnsomeone/someproject.gitIf you want to see information about your remote servers you can do:

If you want to view the remote server information, you can:

# shows URLs of each remote server$ git remote -v # gives more details about each$ git remote show name You can always see the differences between a local branch and a remote branch:

You can always see the difference between the local branch and the remote Branch:

$ git diff master..john/masterYou can also see the changes on HEAD that aren’t on that remote branch:

You can also see the changes to the HEAD pointer that are not available on the remote Branch:

$ git log remote/branch..# Note: no final refspec after ..
6. Tagging label

There are two types of annotations in Git: lightweight annotation and annotation.

Remember, the second is the pointer base of Git. The difference between the two is simple. Lightweight annotation is the pointer for simple name submission. You can point it to another commit. Annotation is a name pointer that has information and history and points to the annotation object. It has its own information. If necessary, you can perform GPG marking.

It is easy to create two types of labels (one of the command lines has been changed)

$ git tag to-be-tested$ git tag -a v1.1.0 # Prompts for a tag message
7. Creating Branches create Branch

Creating a branch in git is very simple (it is very fast and only requires less than bytes of file size ). Create a new branch and switch to the branch, which is usually as follows:

$ git branch feature132$ git checkout feature132

Of course, the most direct way to switch to this branch is to use the following command:

$ git checkout -b feature132

If you want to rename a local branch, It's easy:

$ git checkout -b twitter-experiment feature132$ git branch -d feature132

Update: Or you (Brian Palmer pointed out in the original blog comment) can use-m to switch to "git branch" (as Mike pointed out, if you only need a specific branch, you can rename the current branch)

$ git branch -m twitter-experiment$ git branch -m feature132 twitter-experiment
8. merge branches

In the future, you may think about merging your changes. There are two ways to do this:

$ git checkout master$ git merge feature83 # Or...$ git rebase feature83

The difference between merge and rebase is that merge will try to fix the changes and create new commits to integrate them. Rebase is to merge the changes after your last separation from the other branch, and directly follow the head pointer of the other branch. However, do not use rebase before you push a branch to a remote server. This will confuse you.

If you cannot determine which branch (which need to be merged and which need to be removed ). Here are two git branch switching methods to help you:

# Shows branches that are all merged in to your current branch$ git branch --merged# Shows branches that are not merged in to your current branch$ git branch --no-merged
9. Remote Branch

If you want to place the local branch on the remote server, you can use this command for pushing:

$ git push origin twitter-experiment:refs/heads/twitter-experiment# Where origin is our server name and twitter-experiment is the branch

If you want to delete a branch from the server:

$ git push origin :twitter-experiment

If you want to view the status of the remote Branch:

$ git remote show origin

This will list remote branches that once existed but do not exist, which will help you easily Delete unnecessary local branches.

$ git remote prune

Finally, if the local tracing remote branch is used:

$ git branch --track myfeature origin/myfeature$ git checkout myfeature

In this case, the new version of Git will start automatic tracing. If you use-B for checkout:

$ git checkout -b myfeature origin/myfeature
Storing Content in Stashes, Index and File System store Content, indexes, and File systems in stash 10. Stashing

In Git, you can save the content of the current workspace to the Git stack and read the relevant content from the most recent commit. The following is a simple example:

$ git stash# Do something...$ git stash pop

Many people recommend using git stash apply instead of pop. In this way, the stored stash content will not be deleted after recovery, and the stored stash content will also be deleted when 'pop' is restored, you can use git stash apply to remove the latest content from any stack.

<code data-language="javascript">$ git stash drop</code>

Git can automatically create commands based on the current submitted information. If you prefer general information (equivalent to not making any changes to the previous commit)

<code data-language="javascript">$ git stash save "My stash message"</code>

If you want to use a stash (not necessarily the last one), you can display its list and then use:

<code data-language="javascript">$ git stash list  stash@{0}: On master: Changed to German  stash@{1}: On master: Language is now Italian$ git stash apply stash@{1}</code>
11. Add Interaction

In svn, if your file has been changed, all the changed files will be submitted. In Git, in order to better submit a specific file or patch, you need to submit the content of the selected submitted file in interactive mode.

$ git add -istaged     unstaged path*** Commands ***  1: status      2: update   3: revert   4: add untracked  5: patch      6: diff     7: quit     8: helpWhat now&gt;

This is a menu-based interactive prompt. You can use the number before the command or enter the highlighted letter (if you have highlighted input) mode. The common form is to enter the number before the operation you want to perform. (You can execute commands in the format of 1, 1-4, 2, 4, and 7 ).

If you want to enter the patch mode (enter p or 5 in interactive mode), you can also perform the following operations:

$ git add -p    diff --git a/dummy.rb b/dummy.rb  index 4a00477..f856fb0 100644  --- a/dummy.rb+++ b/dummy.rb@@ -1,5 +1,5 @@ class MyFoo   def say-    puts "Annyong Haseyo"+    puts "Guten Tag"   end endStage this hunk [y,n,q,a,d,/,e,?]?

As you can see, you will get some options at the bottom of the part of the file you selected to add the changes. In addition, use "?" This option is described.

12. Storage/retrieval in the file system

Some projects (such as Git projects) Need to directly add additional files that do not want to be checked in the Git file system.

Let's start saving random files in Git

$ echo "Foo" | git hash-object -w --stdin51fc03a9bb365fae74fd2bf66517b30bf48020cb

For example, if you do not want to recycle objects in a database, the simplest way is to add tags to them:

$ git tag myfile 51fc03a9bb365fae74fd2bf66517b30bf48020cb

Here we set the myfile tag. When we need to retrieve this file, we can do this:

$ git cat-file blob myfile

This is useful for useful files (passwords, gpg keys, etc.) that developers may need but do not want to check every time (especially in the production process ).

Logging and What Changed? What changes have been recorded and logged?

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page

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.