Git Learning Summary

Source: Internet
Author: User
Tags git client using git git commands git revert commit

See better information, http://www.cnblogs.com/zyf-zhaoyafei/p/4486220.html.

Recently learned to learn git, summed up a little bit about the use of GIT commands and problems encountered
Git is known to be the most advanced distributed version control system in the world today. The following is a git usage knowledge


What is the main difference between SVN and git?
Talking about git naturally think of SVN, I have just access to the version Control tool is SVN,SVN is a centralized version control system, the repository is centralized in the central server, when working, first from the central server where to get the latest version, after the completion of the work, you need to put the work done to the central server. Central server is like a picture library, you have to change a book, you must first borrow from the library, and then go home to change their own, changed, and then put back to the gallery. Centralized version control system must be networked to work, if the LAN can also, the bandwidth is large enough, fast enough, if under the Internet, if the speed is slow, it will delay the matter.
Git is a distributed version control system, it does not have a central server, everyone's computer is a full version of the library, when the work is not required to network, because the version is on their own computer. Since everyone's computer has a full repository, how do people collaborate? For example, they changed the file a on the computer, the other people also on the computer changed the file a, at this time, you two only need to put their changes to each other, you can see each other's changes.
Compared with centralized version control system, the security of Distributed version control system is much higher, because everyone's computer has a complete version of the library, a person's computer is broken do not matter, casually from other people to copy one. Centralized version control system central server if there is a problem, everyone can not work.
The basic common commands for Git are:


Ii. Common git commands

--File Directory Operations Command

1 mkdir * *: Create an empty directory * * refers to directory Name 2 pwd:      Displays the path to the current directory. 3 Cat * *     View * * File contents 4 git RM * *  Delete * * file

--git initialization operation

1 git init   turns the current directory into a manageable git repository, creating a hidden. git file. 2 git Remote add origin https://github.com/git/librarysystem.git  pushes the contents of the local repository to the GitHub repository. 3 git clone [email protected]:michaelliao/gitskills.git   clone from remote library 4 git Add * *          add xx file to Staging area. 5 git commit–m "* *"  

--git Cloning Branch

1 git clone xxx.git    //simplest direct command 2 git clone xxx.git "Specify directory"//clone to the specified directory 3 git clone-b [new_branch_name]  xxx.git//cl One creates a new branch instead of the default Origin HEAD (master)

--clone Remote Branch

  the git clone command defaults to only the master branch , and if you want to clone a remote branch (for example: Dev), you can do the following:

1. View all branches (including hidden) git branch-a Show all branches

1 * master2   remotes/origin/head, Origin/master3   remotes/origin/dev4   remotes/origin/master

2. Create a new branch with the same name ("Dev") locally and switch to that branch

1 git checkout-t origin/dev This command is equivalent to: 2 git checkout-b dev Origin/dev

--View commands

1 git status        view warehouse status 2 git diff  * *      view xx file modified those content   3 git log          view history 4 git Reflog       

--Version fallback

1 git reset  –hard head^ or git reset  --hard head~ fallback to previous version 2  (if you want to fall back to 80 versions, use git reset–hard head~80  git R ESET--hard version number)

--undo changes

1 Checkout 2 Restore a modified file (undo uncommitted modifications): 3 $ git checkout file-name 4  5 revert 6 revert a committed modification (modified that has already been committed) 7 revert the last-commit modification: 8 $ git Revert HEAD 9 Restore the specified version of the modification: $ git revert Commit-id

--Branch management

1 git push–u (do not need to use-u for the first time) Origin master pushes the current master branch to remote Library 2 git branch           View all current branches 3 git branch name      Create branch 4 git chec kout-** the    xx files in the work area of the changes are all revoked. 5 git checkout–b dev  create dev branch and switch to Dev Branch 6 git checkout Master  switch back to Master branch 7 git branch–d dev    remove Dev branch 8 git Merg E Dev        merges the Dev branch on the current branch 9 git push--set-upstream origin Dev commits the modification and creates the remote branch dev

--Hidden files

1 git stash       hide the current job and wait to resume the scene after resuming the live 2 git stash list  view all hidden files Listing 3 git stash apply recover hidden files, but content does not delete 4 git stash Dro P  Delete files 5 git stash pop   recover files while also deleting files

--View Remote library information

1 git remote      to view information about a distant library 2 git remote–v   view Remote Library details

--git Pull Operation

The purpose of the 1 git pull command is to retrieve an update from a branch of a remote host and merge it with the local specified branch, in the following basic format. 2 $ git pull < remote hostname > < remote branch name >:< local branch name > 3  4 Retrieve the next branch of the origin host and merge with the local Master branch, which needs to be written as follows 5 $ git pull orig In Next:master 6  7 If the remote branch is merged with the current branch, the part after the colon can be omitted. 8 $ GIT pull origin next 9 10 The above command indicates that the Origin/next branch is retrieved and then merged with the current branch. Essentially, this is equivalent to doing git fetch first and then git merge. One $ git fetch origin12 $ git merge origin/next13 14 in some cases, Git automatically creates a tracking relationship (tracking) between the local branch and the remote branch. For example, in Git clone, all local branches default to the remote host with the same name branch, establish a tracking relationship, that is, the local master branch automatically "track" Origin/master branch. Git also allows you to manually create a tracking relationship. The Git branch--set-upstream master origin/next17 18 above command specifies the master branch trace Origin/next branch. If the current branch has a tracing relationship with the remote branch, git pull can omit the remote branch name. $ GIT pull origin


three, often error:
1:there is no tracking information for the current branch ...
The link relationship between local branch and remote branch is not created, with the command:
Git branch--set-upstream branch-name origin/branch-name.

2:! [Rejected] Dev, dev (non-fast-forward) ... Updates were rejected because the tip of your current branch
Push failed because the latest commit of remote code conflicts with the commit you are trying to push, the workaround is simple, first use git pull to capture the latest commit from Origin/dev, then merge locally, resolve the conflict, and then push

3:conflict (content): Merge CONFLICT in ...
This time git pull succeeds, but there is a conflict in the merge, which needs to be solved manually, and the workaround is the same as the conflict resolution in branch management. After resolving, submit, and then push:

4:you is not currently in a branch, so I cannot with any
WORKAROUND: Git checkout Master

5:your Branch is behind ' Origin/master ' by 1 commits, and can be fast-forwarded.
The above display means that there is an update that has not yet reacted locally, possibly someone else has submitted something to the server.
You can use the git pull command to get these updates locally.

6: You may see the following message when you execute a git push:

Warning:push.default is unset; Its implicit value was changing Ingit 2.0 from ' matching ' to ' simple '. To squelch this messageand maintain the current behavior after the default changes, Use:git config--global push.default m Atchingto squelch This message and adopt the new behavior now, use:  git config--global push.default simpl

Workaround: The ' matching ' parameter is the default behavior of Git 1.x, and if you do git push without specifying a branch, it will push all your local branches to the corresponding matching branch in the remote repository. The default for Git 2.x is simple, meaning that when you do not specify a branch for git push, only the current branch will be push to the code you get using git pull.
Follow the prompts to modify the git push:

1 git config--global push.default matching

You can do git push again.

7:you asked to pulling from the "origin" of the remote, but does not specify: a branch. Because This isn't the default configured remotefor your current branch, and you must specify a branch on the Comman D line.

Workaround: Find:.git/config  修改如下

1 [branch "master"]2   remote = origin3   merge = Refs/heads/master

8:error:permission to User1/test.git denied to user2 fatal:the remote end hung up unexpectedly

Account conflicts, which are explained below


Iv. Experience of experience:
The difference between 1:git revert and git reset
Starting to feel the same, but after the client and command-line window operation found a big difference, see an example: I first submit three times the three-way ID number is (15d5f70,a167eb1,8fbfe3f) as

Then perform version fallback git reset–hard a167


Instructions to perform git reset–hard * * Version fallback is to undo a commit, but the subsequent changes will be returned to staging area, as it shows a commit (by 1 commit)
Similarly execute git revert–hard a167
Git revert is a commit before rolling back with a new commit
In the rollback operation, the effect is the same. However, there is a difference when you continue to merge old versions of the previous version later. Revert is a reverse commit "in and" before the commit, so in the future when merging old branch, resulting in this part of the change will not reappear, but reset is to delete some commits on a certain branch, so with the old branch again merge , the commits that are rolled back should also be introduced.
Get the conclusion: Git reset is to move head backwards, and git revert is head moving forward,
Only the content of the new commit and the content to be revert is the opposite, which can counteract the content to be revert.

V. Common changes:
1: Avoid entering user name and password for each commit
The reason is to use the HTTPS mode puh in Termail inside the git remote-v can see the shape of the return results
Origin Https://github.com/git/librarysystem.git (Fetch)
Origin Https://github.com/git/librarysystem.git (push)
Here's how to change it to SSH.

2: Add ignore file. Gitignore
The. Gitignore configuration file is used to configure files that do not need to be added to version management.
Configuration syntax: "/" At the beginning of the directory; "*" wildcard with multiple characters; "?"  Wildcard single character "[]" contains a single character matching list; "!" Indicates that the matching file or directory is not ignored (tracked);
Description: Git does a rule match on a. Ignore profile in rows from top to bottom, meaning that if the previous rule matches a larger range, the subsequent rules will not take effect;
Example: (1) foder/* ignores all content under directory Foder, whether it is the/foder/directory under the root directory or a subdirectory/list/foder/directory, is ignored.
(2)/foder/* Description: Ignore the root directory of the/foder/directory of the entire contents;
(3)/*!. Gitignore
!/fw/bin/
!/fw/sf/
Description: Ignores all content, but does not ignore. gitignore files,/fw/bin/and/fw/sf/under the root directory
 Note: the. gitignore file only works for files that have not yet been added to version management, and if you have previously used git to include files that you want to ignore in the repository, it will not work.

3: How to use two git accounts on the same computer

Problem Description: As programmers sometimes have multiple projects, and each project git user name, mailbox, etc., are different. As far as I am concerned, I have an account on GitHub, I develop some things, and I also participate in the project need to connect other people's GitHub account, on my computer only a git client, a local identity user, the beginning of my own development is the SSH way, Participate in the project is to use HTTPS connection, but https each need to enter a password, very inconvenient, so want to turn into an SSH connection, using the command:
>$ git remote RM origin
>$ git remote add origin [email protected]:yu/demo.git
>$ git push origin
The conversion was successful, unfortunately the push was wrong: Error:permission to User1 denied to
The workaround for this scenario is to use a local host for multiple SSH keys, with different accounts using different keys.
First step: Generate SSH Key

Execute commands, such as:

Specific Description:

1 $ ssh-keygen-t rsa-c "[email protected]" 2   creates a new SSH key using the provided email generating public/private RSA key Pair.3   #输入将要保存的路径, my default is the current path (/C/USERS/ZHAOYAFEI/.SSH/ID_RSA), but cannot use the default file Id_rsa, to add a new account will be born a new friendly name, For example Id_rsa_work4   Enter file in which to save the key (/C/USERS/ZHAOYAFEI/.SSH/ID_RSA): 5   #输入回车后提示输入一个类似于密码的号, Just enter the line 6   enter passphrase (empty for no passphrase): 7   Enter same passphrase again:

Step two: Identify the new private key
The default SSH only reads Id_rsa, so in order for SSH to recognize the new private key, it needs to be added to the SSH agent

1 Ssh-add ~/.ssh/id_rsa_work2  //This command if error: Could not open a connection to your authentication agent. Executable ssh-agent Bash command After the execution of Ssh-add-l to see if the success.

Execute commands, such as:

Step three: Modify the config file
This file is used to configure the server that corresponds to the private key. The contents are as follows:

1 # Default GitHub 2       Host github.com 3       HostName github.com 4       User git 5       identityfile ~/.ssh/id_rsa 6  7 # Second user ([email protected]) 8       Host github_work 9       HostName github.com10       User git11       identityfile ~/.ssh/id_rsa_work

This configuration, that is, using hostname as github.com will be based on the user name of the different, to use the private key not used. On GitHub, you can also add a corresponding public key. In fact, this configuration is about the SSH is not related to git, but git is the way to use SSH. Host can be arbitrary, convenient for their own memory, follow-up in the addition of remote is also needed.


Fourth step: Enter SSH key into the GitHub website
You can enter the generated id_rsa_work.pub into the GitHub site, and after you click Add key after entering it, you will see that git-tutorial has been added. After the configuration is complete, when connecting to a non-default account of the GitHub warehouse, the remote library address to do some corresponding changes, such as adding a warehouse test under the ABC account now, you need to add: Git remote add test [email protected]:abc/ Test.git #并非原来的 [email protected]:abc/test.git so that each connection uses ID_RSA_ABC to connect to the server.


  Note:GitHub is based on the configuration file User.email to get the GitHub account display author information, so for multi-account users must remember to change the user.email to the corresponding email ([email protected]). The problem I encountered was a local single user, linked to a different GitHub account, so the user configuration could be configured globally. If different local users (different identities, that is, need to use a different user name locally), then git does not use the –global configuration, you can follow the following method:

1 1) Set local user.name and user.email2     git config user.name "xxxxxx" 3     git config user.email "[email protected]" 4 2) Set global user.name and user.email5     git config--global user.name "xxxxxx" 6     git config–-global user.email ""


Vi. digression (Related concepts)

Git-Version Control tools
Git is an open source distributed version control system for efficient, high-speed processing of project versioning from very small to very large. Git is an open source version control software developed by Linus Torvalds to help manage the development of the Linux kernel. Torvalds started to develop Git as a transitional solution to replace BitKeeper, which was previously the primary source code tool used by Linux kernel developers worldwide. Some in the open source community felt that BitKeeper's license was not appropriate for the open-source community, so Torvalds decided to work on a more flexible version control system. Although Git was originally developed to assist the Linux kernel development process, we have found that Git is also used in many other free software projects. For example, recently migrated to Git, and many freedesktop projects migrated to Git.

Github-A Web site that provides user space to create a git repository, save some of the user's data documents or code, etc.
As an open source code base and version control system, GitHub currently has more than 1.4 million developer users. As more and more applications move to the cloud, GitHub has become the preferred method for managing software development and discovering existing code. As mentioned earlier, as a distributed version control system, there is no concept of the main library in Git, and each copy of the library can be used independently, and any inconsistencies between the two libraries can be merged. GitHub can host a variety of git libraries and provide a web interface, but unlike other services like SourceForge or Google code, GitHub's unique selling point is the simplicity of branching from another project. Contributing code to a project is simple: first click on the "Fork" button of the project site, then check out the code and add the changes to the code base you just separated, and finally request a code merge from the project owner through the built-in pull request mechanism. GitHub has already been called the code player's MySpace.

GitLab-git-based project management software
GitLab is an open source project for the warehouse management system. Use Git as a code management tool and build on this Web service. All three are git-based and can be said to be git derivatives.

Git Learning Summary

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.