Embrace the power of open source--git Quick Access Tutorial

Source: Internet
Author: User
Tags git workflow using git

What is git

Git is currently the most advanced Distributed version control system in the world. Originally written by Linus Torvalds, used as the management of Linux kernel code. If you are a Windows user, you may be worried about "is it only available on Linux?" Or git is not so friendly to Windows. "And then I tell you that your fears are superfluous, GitHub released GitHub for Windows, providing an easy-to-use git graphical Client for Windows platform developers; Microsoft also provides Git version control to developers through CodePlex.

Why learn Git

In addition to using GIT to manage your own projects, and because more and more people are using GitHub to host their own open source projects, you can find open source projects that are right for you (here are some articles, "using open source projects, learning software development") where you can find fellow humans, Join a team to improve yourself. At the same time GitHub can also help you find a satisfactory job!! Yes, there may be a company that sees you on a project hosted on GitHub and gives you an offer, and your github may be a plus in your resume.

Install settings git

1.Msysgit is the Windows version of git click Download installation completed, click on the Start menu-"Git folder-" Git Bash will appear below the command line form, prove that you installed successfully.

2. set your name and email, your name and email will appear in your submission record.

git config--global " Your name "-global" your email "

Note: When git config uses the--system parameter, git reads and writes the/etc/gitconfig file that contains the configuration values that are valid for all users on the system and the warehouses they own

When git config uses the--global parameter, git reads and writes the ~/.gitconfig file that contains the configuration values that apply only to that user

When git config uses the--local parameter, git reads and writes the configuration file (. git/config) from the Git directory in each library that is defined by the user, which contains the configuration values that apply only to that Git library

The three-tier configuration described above is advanced from the general to the special layer, if the defined values are conflicting, whichever is defined in the later layer, for example: conflicts in. Git/config and/etc/gitconfig are used. Git/config value

You can use the following command to produce configuration values for Config

git config--list

3. Initializing the new warehouse in the working directory

Warehouse, English name repository, you can easily understand a directory, all the files in this directory can be managed by git, each file modification, deletion, git can track.

First we go to the Repository folder from the command line:

Use

Git init

command to turn a folder into a repository that git can manage.

In this case, the repository directory is one more .git directory (if not, perhaps your Windows settings do not show hidden files, folders, in the folder options to modify the settings just fine.) , this directory is git to track the management of the repository, do not manually modify the directory inside the file, or change the mess, the Git repository to destroy.

We can also clone from an existing warehouse

git clone http://git.oschina.net/xxxxxx/xxxxxx.git

Instead of using clone checkout, Git to collect all the data from the project history (each version of each file), there are local data clones on the server. In fact, even if the server's disk fails, any cloned client can rebuild the repository on the server and back to the original state of the clone.

We're going to clone the yard looking for a harmonious big Papapa project:

I fork this project first and get his address:

Start Clone:

Clone complete, and look at the folder for the warehouse:

Git various actions

git Add File

Before learning how to add a file, let's look at the git workflow:

Your local repository consists of three "trees" maintained by git. The first one is your working directory, it holds the actual file, the second is the buffer (Index), it's like a cache area, temporarily saves your changes, and finally the HEAD, which points to the result of your last commit.


First use the git add command to commit the planned changes to the buffer,
Then use the GIT commit-m Code submission command to submit the code to the head, which is not yet committed to the server,
Execute the GIT push Origin Master command to submit these changes to the server, and you can replace master with any branch you want to push.

Let's try to add a file

1. I've created a new Readme.txt in the Warehouse folder.

2. Use the command git add Readme.txt to go inside the cache.

3. Use the command git commit-m to submit the file to head.

4. Use git status to check the current file status

git commit Changes

1. First, let's revise the Readme.txt.

2. We use GIT status to view the results:

The above information tells us that Readme.txt has been modified, but not yet ready to submit the changes.

3. We can use git diff + file name to view the content of the specific changes.

4. Commit the modified file using Git commit.

Git version fallback

1. Use git log to query the history of submissions

2. If you feel too much information displayed, we can use the git log--pretty=oneline command

The first column of a large string of characters is the commit ID, which is a very large number computed by a SHA1, in hexadecimal notation, and the second column as the comment we filled in when we submitted it.

3. Then we fall back to the last version can use the git reset--hard head^ command, back to the previous version of Git reset--hard head^^, and so on, when we back 100 versions, can be written as git reset--hard HE ad~100

You can see that the head is currently pointing to the version with the commit ID 9d3830c.

4. We have rolled back a version, we can revoke this fallback of course, we use git reset--hard +commit ID version number (no need to write full, the first few can, git will automatically go to find).

You can see the head again pointing back to the version with the commit ID 4332ee4.

git delete files and recover files

1. We can use git rm+ file name command to delete files

2. We use the git status command to look at the current warehouse status

Can see deleted: shows the file we just deleted, we look at the Warehouse folder, found that Readme.txt is not.

3. We then use the git checkout command to recover the file that was just deleted.

git checkoutInstead, replace the workspace version with the version in the repository, which can be "one-click Restore", regardless of whether the workspace is modified or deleted.

Add a remote repository

Add SSH Keys

SSH keys is designed to prevent anyone from being free to clone or push code.

SSH keys and keys, you generate the key and the key in the local, and your public key to the server or other collaborators, then you can in their Git repository clone and push code and so on. A collaborator machine corresponds to an SSH key relative to a single server. 1. First Use $ ssh-keygen-t rsa-c "Your mailbox"1. Here is the location where you enter your SSH keys to save, which is the path in the front brackets. 2. Enter the password, you can directly enter. 3. Enter the password again and you can return directly. Open the folder you just saved with SSH keys: Id_rsa is the private key, Id_rsa.pub is the public key, we can/id_rsa.pub the cat path to view the public key. You can also open a file with the file Manager. 2. Add your public key on GitHub or [email protected]

Remote Warehouse

1. Create a new warehouse in [email protected]

2. After the creation is complete, add the command after use: Git remote add origin [email protected]: your username/gitstudy.git

The name of the remote library is that origin it is the default term for git, or it can be changed to something else, but origin this name is known as a remote repository.

Next, you can push all the contents of the local library to the remote library:

3. Use to $ git push -u origin master push all the contents of the local library to the remote library

Summary

About Git, this article also did not talk about branches, interested students first to see Liaoche Teacher's Tutorial bar, this article will also update this part of the content, the Spring festival holiday so hastily passed, feel did not learn anything, hope to continue efforts. Can stick to the habit of blogging.

Resources

1. Liaoche Teacher's git tutorial

2.Git Easy to use tutorial: http://www.bootcss.com/p/git-guide/

3. Open source China [email protected] Help document

4.Pro Git (Chinese version): http://git.oschina.net/progit/index.html

Embrace the power of open source--git Quick Access Tutorial

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.