How to use git commands

Source: Internet
Author: User
Tags file info how to use git version control system git commands

Git install package

Http://c35.yunpan.360.cn/my/?sid=#%2F%E5%AE%89%E8%A3%85%E5%8C%85%2FGit%E5%AE%89%E8%A3%85%2F

GIT installation key generation steps
 1 . Git config--global user.name xxx  2 . Git config--global User.email [email  Protected]  3 . ssh-keygen -C [email protected]-t RSA 
The 3rd step is divided into the following 3 small steps ( 1 ) public key save Address press ENTER to select default ( 3 ) generates public and private keys

Identification has been saved In/c/users/xxx/.ssh/id_rsa.
Public key has been saved in/c/users/xxx/.ssh/id_rsa.pub.

//git public key------/c/users/xxx/.ssh/id_rsa.pub. Contents of the file
Ssh-rsa aaaab3nzac1yc2eaaaadaqabaaabaqc/pyfukmzupnjtjodsql74gh4jxaq8f+eqamor9yw+ 5vnw81ewrrbbssjpedpazibb0sc89oj4uzi2lphjhtujmmdzf2trfoagra2+m5ey4z1mxut4rvawrz/ptk7tti+emkc
/nzqhcp9e+nyivzbwumqvanx+otgdf3rymywdcbvtx0aybobd/nwv1btr5pkprhlgqn0oi5whmvxmp9wiq9daoi+w63mhl+c8a8qu+ pbzbiknzjrb1p9dtioepdq5ltgaqluez0ftezet1qh0pauaoxxeszahgpa9453rqj433
EMG7HY30KOMOQVFP64T6QLTGFKIG9EMM9T8NY2R [email protected]

Git protocol Myths
Use the SSH protocol to avoid push entering a password every time. The prerequisite is to configure the SSH key in the account. [email protected] begins with the SSH protocol. Using the HTTPS protocol causes the user name and password to be entered each time.



git Bash Command window and font size settings

Mouse click on the Git Bash screen title bar, right mouse button Select

Window size Settings

Options--Windows--Modify row count (height) + Number of columns (width)

Window font settings

Options--text--Set font size

Other settings can refer to this article

http://my.oschina.net/lieefu/blog/476252

Solve the problem of Chinese garbled gitk under Windows

http://howiefh.github.io/2014/10/11/git-encoding/

Let's take a look at the command window settings for Win7

Http://article.pchome.net/content-1245289-all.html

Create a local library
Create from scratch
1. Git init creates an empty git library. Produces a. Git subdirectory in the current directory. In the future, all file changes will be saved to this directory 2. Git init-bare Create a remote repository (a backup warehouse that can be created in a path other than the server or the project directory)

Cloning an existing warehouse creation
git clone/path/to/repository                                                             //local repository git clone  git/ssh/http[s]/ftp[s]/file/rsync:[email protected]/path/ To/repository               //remote server

The essence of Git clone is to copy the contents of the "Git directory", and there are thousands of objects in the "Git Directory" (Commit object, tree object, binary object, Tag object ...), the efficiency can be imagined.

If you transfer through the Git, SSH protocol, the server will send the various objects that need to be transmitted before transmission, and the HTTP (s) protocol will repeatedly request the different objects to be transferred. If there are few submissions in the warehouse, the efficiency of the former and the latter is not much different;

However, if there are many commits in the repository, the Git and SSH protocols will be more efficient to transfer. But now git has made some optimizations for the HTTP (s) protocol transfer git Repository, and HTTP (s) transfers can now achieve the efficiency of the SSH protocol.

For details, please refer to the creation and composition of the local warehouse

Http://www.infoq.com/cn/news/2011/02/git-adventures-local-repository

The composition of the. Git directory

Commit_editmsg # holds the comment information from the last commit config # project configuration Information Description # project description Information Head # The information of which branch the project is currently in
Index # Indexed file, git add after the content to be added to temporarily exist here hooks/ # Default "hooks" script file info/ # There is a exclude file, specify the file to ignore the project #, see here logs/ # Historical information of each refs objects/ # This directory is very important, which is stored in Git data Objects # includes: Commit (commits), tree object (trees), binary object # (BLOBs), Tag object (tags).
# in the objects directory, the first two characters of the SHA string as the directory name, followed by the 38 characters as the file name
refs/                       # identifies which commit (commit) each of your branches points to.

View the contents of the commit file, 2d832d is the SHA (Secure Hash algorithm) signature value
$ git cat-file-p 2d832d

Work flow

Local and remote workflows

Your native code consists of three "trees" maintained by git. The first one is yours 工作目录 , it holds the actual file, and the second is 缓存区(Index/stage) that it's like a cache area, temporarily saving your changes, and finally the local repository HEAD) , which points to the results of your most recent commit.

git clone is used to create a local repository//origin is the remote hostname used by git clone by default
After obtaining the remote Git repository via git clone, the. Git/config developer information will not be clone together. You still need to add developer information for the. git/config file for your local library. In addition, developers need to add them themselves. Gitignore file.
The remote git repository obtained through Git clone contains only the current working branch of the remote Git repository. If you want to get other branch information, you need to use "Git branch–r" to see, if you need to get other remote branch code, you can use the command "git checkout-b local branch name Remote branch name",
The branch name listed in the remote branch named "Git Branch–r" is typically the same as "origin/branch name". If the local branch name already exists, then the "-B" parameter is not required.
git fetch

Get the latest version from remote to local, not automatically merge, more secure than git pull.
For example, if you use Git checkout Nov/eclair_rocket (Nov/eclair_rocket is the branch name on the server), get the code that was downloaded from the server when you last used the git fetch command;
If you use Git fetch first, and then Git checkout nov/eclair_rocket, you get the latest updates from the server and download the latest code from the server.


Git pull
Get the code from the server's warehouse, and merge the local code. Equivalent to: git fetch + git merge
This command takes the remote branch name from the remote Git repository to a local branch of the local git repository. Where the local branch name is not written, the default pull is to the local current branch. If your local branch already has content, git pull merges the files and alerts you if there is a conflict.
git pullAndgit pushIs the operation for all branches. Which means even if I switch to a sub-branchalphaIf the branch and the branch of the far end are already associated, then the executiongit pullWill putmasterBranches andalphaBranches are updated to the far end.

Add and submit

You can schedule changes (add them to the cache) and use the following command:

git add dir1           add dir1 This directory, all files in the directory are added
git add f1 f2 *
Git add-a Add all the files in the current directory
git Add. Add all files and subdirectories under the current directory
git rm --cache a.c          Delete files that have been submitted to the local warehouse

This is the first step in the Git basic workflow, using the following command to actually commit the changes:

" Code Submission Information "
Git commit-a-M "Code submission Information"    git commit-a is to turn unstaged files into staged (this does not include the new (untracked) file), and then commit

Now, your changes have been submitted to HEAD, but not to your remote repository.

Push changes

Your changes are now in the HEAD of the local repository. Execute the following command to commit the changes to the remote repository:

Git push Origin Master
Git push does not automatically merge files. Therefore, if there is a conflict with git push, it will be forcibly overwritten with the contents of the post-push file, and there is no hint. This is a very dangerous thing to do in co-development.


You can replace master with any branch you want to push.

If you have not cloned an existing repository and want to connect your warehouse to a remote server, you can add it using the following command:

Git remote add Origin <server>

So you can push your changes to the server you've added.

Branch

Branches are used to insulate the development of features. When you create a warehouse,master is "default." Develop on other branches, and then merge them onto the main branch when you are finished.

Operation detailed Command
New Branch git branch testing
Switch branches git checkout testing
Delete Branch git branch -d testing
View Branches git branch

Create a branch called "feature_x" and Switch to the past:

Git checkout-b feature_x

Switch back to the main branch:

git checkout Master

Then delete the new branch:

Git branch-d feature_x

Unless you push the branch to the remote repository, the branch is not visible to others :

Git push Origin <branch>

// Renaming a local branch  -M  branch-old-name  branch-new-name

Update

To update your local repository to the latest changes, do the following:

Git pull  Origin <remote-branch> <local-branch>
Git fetch Origin <remote-branch>
Label

It is recommended to create a label when the software is published. This is an old concept, also in SVN. You can execute the following command to create a label called 1.0.0 :

1.0. 0 1b2e1d63ff

1b2e1d63ff is the first 10 characters of the commit ID you want to tag. Use the following command to get the commit ID:

git log

You can also use the submission ID for fewer of the first few, as long as it is unique.

// add comments to tag  -A v1. 4 ' my version 1.4 '

Show Tag version record
Git show Tag-name

Push all tags to a remote server at once
Git push Origin--tags
Take out a version of the tag
git checkout-b branch_name tag_name
// When you delete tag, be aware that if you submit tag to the remote server, delete the tag, not only the local tag, but also delete the remote tag // the Delete local tag command is  -D tag-name//  the command to delete the remote tag is  --delete tag-name
Replace local changes

If you do something wrong (naturally, this is not possible), you can replace the local change with the following command:

Git checkout--<filename>

This command replaces the files in your working directory with the latest content in the HEAD. Changes that have been added to the cache, as well as new files, are unaffected.

If you want to discard all of your local changes and commits, you can get the latest version on the server and point your landlord branch to it:

--hard Origin/master

Git ignores how files are set

Refer to the following two URLs

Three ways to set up git ignore files

Http://www.jianshu.com/p/267cd94f1d49

Specific syntax for git ignore file settings

http://www.cnblogs.com/pylemon/archive/2012/07/16/2593112.html#3226325

Focus on the pits that have been trampled:

. gitignore files can ignore themselves. Ignored files are valid only for non-tracked files and are not valid for files that have been added to the repository.

Git check-ignore can be used to check that the written ignore file syntax is correct

The principle of ignoring files is:

    1. Ignoring the auto-generated files of the operating system, such as thumbnail images;
    2. Ignoring compile-generated intermediate files, executables, and so on, that is, if a file is automatically generated through another file, then the automatically generated files will not need to put into the repository, such as the Java compiler generated .class files;
    3. Ignore your own configuration file with sensitive information, such as the configuration file that holds the password.

No need to write .gitignore files from scratch, GitHub has prepared a variety of configuration files for us, only need to combine to use, such as input tags and sublimetext, in the following URL to query the Ignore rules

https://www.gitignore.io/

Git branch Merge

Suppose at this point that you suddenly receive a call saying that there is a serious problem that needs urgent repair, you can do it in the following way:

    1. Return to the branch that was previously published on the production server.
    2. Establish a new branch for this emergency repair and fix the problem in it.
    3. After testing, go back to the branch where the production server is located, merge the patch branches in, and then push to the production server.
    4. Switch to the branch that previously implemented the new requirement and continue working.
$ git checkout-b iss53    //Create a new ISS53 branch on the master branch

This is equivalent to performing the following two commands:

$ git branch iss53$ git checkout iss53

// merging hotfix branches into master $ git checkout master$ git merge hotfix

Prompt for conflicts when merging

$ git merge iss53  Auto- in then commits the result.

View of merge Status

$ git Statuson branch masteryou has unmerged paths.   " git commit " ) unmerged paths:  "git add <file> " To Mark resolution)        Both modified:      "git add""git commit-a" )

Any files that contain unresolved conflicts are listed in the non-consolidated (unmerged) state. Git will include standard conflict resolution tags in conflicting files, which can be used to manually locate and resolve these conflicts. You can see that this file contains a section similar to the following:

<<<<<<< HEADid= "footer"id=" Footer "> Contact  us at [email protected]</div>>>>>>>> iss53

You can see the top half of the partition, which is the content in the branch ======= HEAD , the master branch that you merge switch to when you run the command, and the lower part is the content in the iss53 branch. The way to resolve conflicts is to either choose one or you can integrate it yourself. For example, you can solve this by replacing the content with the following:

This solution incorporates a subset of the two branches, and I've also deleted <<<<<<< , ======= and >>>>>>> these lines. After all the conflicts in all the files have been resolved, the run git add will mark them as resolved (in effect, a snapshot is saved to the staging area.) )。 Because once staged, the conflict is resolved.

git branch-d studygit     //  Merge Studygit branch to remove Studygit branch  -D studygit    //  Force Delete studygit Branch

Run again git status to confirm that all conflicts have been resolved:

$ git Statuson branch masterchanges to be committed:   " git reset HEAD <file> " To Unstage)        Modified:   index.html

If you feel satisfied, and confirm that all conflicts have been resolved, that is, enter the staging area, you can use git commit to complete the merger submission. The submitted record is almost like this:

Git-Branching for new and merged

https://git-scm.com/book/zh/v1/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E% E5%90%88%e5%b9%b6

Git repository rollback
1, git checkout the_branch2, git pull3, Git branch The_branch_backup//back up the current situation of this branch4, git reset--hard the_commit_id//Roll the The_branch locally back to the_commit_id5, Git push origin:the_branch//Remove Remote The_branch6, Git push Origin the_branch//re-establish the remote branch with the local branch after rollback7, Git push origin:the_branch_backup//If all succeeds, delete this backup branch
--    softfallback Commit--mixed fallback    commit and stage--hard     fallback commit stage workspace   Local Warehouse fallback to a version  --Hard//  Force remote Warehouse fallback to a version git push Remote host name remote directory   -F

If too many output information, see dazzling, you can try to add --pretty=oneline parameters:

git log --pretty=oneline

You see a bunch of similar 3628164...882e1e0 is commit id (version number), and SVN is not the same, Git is commit id not a three-way ... Incrementing the number, but a SHA1 calculated by a very large number, in hexadecimal notation, and you see commit id and my affirmation is not the same, with your own prevail. Why do commit id we need to use such a large number of numbers to express? Because Git is a distributed version control system, we also want to study how many people work in the same repository, if we all use a three-to-one version ... As a version number, it must have been a conflict.

In Git, with the HEAD current version, which is the latest commit 3628164...882e1e0 (note that my commit ID is not the same as yours), the previous version is that the last version HEAD^ is, HEAD^^ of course, 100 versions of the 100 are ^ relatively easy to count. So write HEAD~100 .

Git's version fallback is very fast because git has a pointer to the current version inside, and HEAD when you roll back the version, git just points the head fromappend GPL

The version number is not necessary to write the whole, the first few can be, git will automatically go to find. Of course, you can't just write the top one or two bits, because git might find multiple version numbers, and there's no way to determine which one.

3628164

In Git, there's always a regret pill to eat.

git reflog records every command you've been working on.

    • HEADThe point is the current version, so git allows us to navigate between versions of history, using commands git reset --hard commit_id .

    • Before you travel, git log you can view the commit history to determine which version to fallback to.

    • To return to the future, use the git reflog view command history to determine which version to return to in the future.

Refer to this article version fallback

Http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ 0013744142037508cf42e51debf49668810645e02887691000

git revert

http://samael65535.github.io/git/2013/01/18/git/

Useful tips

Built-in graphical git:
gitk
Color git output:
git config color.ui true
When the history is displayed, only one line of comment information is displayed:
git config format.pretty oneline

To learn more about Git directives, you can read the following two articles

Git entry-level directives

Https://github.com/guodongxiaren/LinuxTool/blob/master/git.md

Git common Command Commentary

http://zensheno.blog.51cto.com/2712776/490748

How to use git commands

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.