Git usage tutorial
What is Git?
Git is currently the most advanced distributed version control system in the world. Originally written by Linus Torvalds, it is used as the management of Linux kernel code. If you are a windows user, you may worry that "is it only applicable to linux? Or Git is not so friendly to windows ." So I will tell you your worries are redundant. GitHub for Windows is released on GitHub, providing an easy-to-use Git graphics Client for Windows platform developers; microsoft also provides developers with a git version control system through CodePlex.
Why Learning Git
In addition to using Git to manage your own projects, more and more people are using Github to host their own open-source projects. You can find an open-source project suitable for you to learn, join a team to improve yourself. At the same time, GitHub can help you find a satisfactory job !! Yes, some companies may send you an offer when they see your GitHub project. Your GitHub may be a plus item in your resume.
Install and set Git
1. msysgit is a Windows version of Git. After you click Download and installation, click Start Menu-Git folder-Git Bash to open the following command line form, proving that you have successfully installed it.
2. Set your name and Email. Your name and Email will appear in your submission record.
Git config -- global user. name "Your name" git config -- global user. email "your Email"
Note: When git config uses the -- system parameter, Git reads and writes the/etc/gitconfig file, which contains the configuration values that take effect for all users on the system and their repositories.
When git config uses the -- global parameter, Git reads and writes ~ /. Gitconfig file, which contains the configuration values only for this user
When git config uses the -- local parameter, Git reads and writes the configuration file (. git/config). The file contains the configuration values that only apply to the Git library.
The three-layer configuration described above advances from general to special layers. If the defined values conflict, what is defined in the later layer shall prevail. For example: In. if there is a conflict between git/config and/etc/gitconfig. git/config Value
You can use the following command to view the config configuration values.
git config --list
3. initialize a new repository in the working directory
Repository, English name repository. You can simply understand a directory. All files in this directory can be managed by Git, and each file can be modified, deleted, and tracked by Git.
First, run the following command to the repository folder:
Use
git init
Command to change the folder into a Git-managed repository.
In this case, an additional.git.), This directory is used by Git to track and manage version libraries. Do not manually modify the files in this directory. Otherwise, the Git repository will be damaged.
We can also clone from the existing warehouse
git clone http://git.oschina.net/xxxxxx/xxxxxx.git
Here we usecloneInsteadcheckout,Git collects all the data in the project history (each file version). After cloning the data on the server, the local data is also available. In fact, even if the server disk fails, any cloned client can be used to reconstruct the repository on the server and return to the status when cloning.
Let's clone the yard to find the great PaPaPa project:
I fork this project to get its address:
Start clone:
Clone finished. Check the folder in the repository:
Git operations
Add files in Git
Before learning how to add files, let's take a look at the Git workflow:
Your local repository is composed of three "Trees" maintained by git. The first is your working directory, which holds the actual file; the second is the cache area (Index), which is like a cache area, saving your changes temporarily; the last is HEAD, point to the result after your last submission
First, use the git add command to submit the planned changes to the cache,
Then, run the git commit-m code submit information command to submit the code to the HEAD. At this time, the Code has not been submitted to the server,
Execute the git push origin master command to submit these changes to the server. You can replace the master with any branch you want to push.
Next we will try to add a file
1.In the warehouse folder, I created a new readme.txt.
2. Run git add readme.txt to add it to the cache.
3. Run git commit-m to submit the file to the HEAD.
4. Use git status to check the current file status
Git submits changes
1. First, modify readme.txt.
2. Run git status to view the result:
We have modified readme.txt, but we are not ready to submit the changes.
3. You can use git diff + file name to view the modified content.
4. Use git commit to submit the modified file.
Git version rollback
1. Use git log to query submitted historical records
2. If you feel that too much information is displayed, you can use the git log -- pretty = oneline command.
In the first column, a large string of characters is the commit id, which is a very large number calculated by a SHA1, expressed in hexadecimal notation, and the second column is the comment we entered when submitting.
3. in this case, we can use the git reset -- hard HEAD ^ command to roll back the previous version of git reset -- hard HEAD ^, and so on. When we roll back the 100 versions, you can write it as git reset -- hard HEAD ~ 100
We can see that the HEAD currently points to the version with the commit id 9d3830c.
4. we have rolled back a version. We can undo This rollback. Of course, we can use the git reset -- hard + commit id version number (you can write the first few digits without having to write it completely, git will automatically find it ).
We can see that the HEAD points again to the version with the commit id 4332ee4.
Delete and Restore Files in Git
1. You can use the git rm + file name command to delete files.
2. Run the git status Command to check the current repository status.
You can see deleted: The file we just deleted is displayed. Then we can see the folder in the warehouse and find that there is no readme.txt.
3. Now we use the git checkout command to restore the deleted file.
git checkoutIn fact, it is to replace the workspace version with the version in the version library. Whether the workspace is modified or deleted, you can "Restore it with one click ".
Add remote Repository
Add SSH Keys
SSH keys is generated to prevent arbitrary code clone or push.
SSH keys has public keys and keys. After you create a secret key and public key, you can tell the server or other collaborators your public keys, then you can clone and push the code in their git version library. compared with a server, a co-author machine corresponds to an SSH keys.1. first, use $ ssh-keygen-t rsa-C "your mailbox" 1. enter the location where your SSH keys are saved, that is, the path in the brackets. 2. Enter the password and press Enter. 3. Enter the password again and press Enter. Open the folder saved by ssh keys: id_rsa stores the private key, and id_rsa.pub stores the public key. We can view the public key in the cat path/id_rsa.pub. You can also use the File Manager to open a file. 2. Add your public key on github or git @ osc
Remote Repository
1. Create a repository in git @ osc
2. After the creation is complete, add and use the command: $ git remote add origin git@git.oschina.net: your username/gitstudy. git
The remote database name isoriginThis is the default Git name. You can also change it to something else,originThis name is known as a remote database.
Next, you can push all the content of the local database to the remote database:
3. Use$ git push -u origin masterPush all content of the local database to the remote database
Summary
About Git, this article does not talk about branches. If you are interested, go to the tutorial by Dr. Liao Xuefeng. This article will update this part of content later, and the Spring Festival holiday will be so hasty, I feel that I have not learned much and hope to continue working hard. Stick to the habit of writing blogs.
References
1. Instructor Liao Xuefeng's Git tutorial
2. Git use simple tutorial: http://www.bootcss.com/p/git-guide/
3. Pro Git (Chinese version ):
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
Git details: click here
Git: click here
This article permanently updates the link address: