How to use Git

Source: Internet
Author: User

window to install Git,
Download the graphics installation package, all the way to the next
: Git-for-windows.github.io

Windows Git integrates a bash that uses GIT commands in this shell

Linux installation is much simpler.

Git easy to use
1 Create a new directory and enter it into the directory
Git init creates a git repository that can be managed by git

2 Place the file in a new directory, or in a subdirectory
git add test.txt adding files to the Warehouse
Note: Do not use Notepad to create a TXT file under Windows, he will add 0XEFBBBF characters to the file header,
Save As Utf-8 without BOM with notepad++ instead of Notepad

3 submitting files to the warehouse
Git commit-m "any description"


4 View the current state of the warehouse
git status


5 View Commit version history
git log

The hexadecimal number of 40 bytes is the commit ID (that is, the version number), and each time a new version is submitted, git actually automatically strings them into a timeline.

6 fallback version
git reset
Git must know which version of the current version it is, with HEAD for the current version, head^ for the previous version,
head^^ indicates how many versions of ^, or head~#, are written on the previous version, #表示多少个版本

git reset--hard head^ (--hard parameter)
git log see now Repository, found that the previous version of the library is not, then go back to the previous version can it?

Git reset--hard hash value of the first few
Command window is not turned off can go up, remember the hash value of each commit, this command so that git will be based on the first few to find the version number, may find multiple version number, it can not be confirmed, then write a few more.
Shuttling between versions, only a commit ID is required. Git log will not find the latest version of the commit ID when it is rolled back to a previous version and Git bash is turned off. git provides a command git Reflog records every time

command, you will be able to find the version number of each operation.
Git reflog

Summary:
The version that head points to is the current version, so git allows us to navigate between versions of history, using the command git reset--hard commit_id.

Before the shuttle, use git log to view the commit history to determine which version to fallback to.

To return to the future, use Git reflog to view the command history to determine which version to go back to in the future.

git principle
1 Work Area
In the directory of the party files on the computer, I create a new test directory to initialize the repository with GIT init, this directory is the workspace

2 Version Library
There is a hidden directory in the workspace. Git, it's the Git repository.
There is a staging area called stage in the Repository, and the first branch master git automatically creates, a head pointer to master

Adding a file to the repository is a two-step move:
Git add adds the modified file to the staging area
Git commit adds all the content in staging area to the current branch in the repository


Three states of the file:
1) Modified, the file has been changed in the workspace, but there is no git add to staging area, git status is shown in red "modified"

2) has been staged, put the modified files to staging area, this time the file is staged, git status shows the green ' modified '

3) submitted, all files git commit, file submitted to Repository, in committed state, GIT status show nothing to commit, work tree clean

3 undo Changes
Git checkout-test.txt undo all changes to the Test.txt file in the workspace, in two cases
1) Test.txt changes are not placed in the staging area, it is the same as the last time to put in the repository is identical
2) Test.txt modified after put to staging area, and modified, and now withdraw back into the staging area state

Summary: Undo always goes back to the last time you put it in the staging area or repository

1 If you modify the workspace file and want to discard the workspace changes, git checkout--test.txt
2 What do I do if I modify and submit a version of the repository? Version fallback
3 If it has been put to staging area, want to give up the changes, how to do? Two-step (focus)
1) git reset HEAD test.txt
2) do as you do


4 Deleting files
Delete files directly in the directory, the working directory is not the same as the repository, Git will know to delete the files, git status tells to delete the files

1) Do you want to delete the version? File git rm in the library, and then git commit
2) If you delete the wrong, the deleted files back, git checkout--test.txt

git checkout this command looks at the Undo command, which is actually a replacement, replacing the state in the staging area or repository with the current workspace

5 Adding a remote repository
Github.com is a Web site hosted on a remote Git repository, first registering an account, creating a warehouse on GitHub, then correlating to the local repository, pushing changes to the local repository to the GitHub repository every time.

1) Create SSH Key. In the user's home directory, see if there is no. ssh directory, if there is, then look at this directory there are no Id_rsa and id_rsa.pub these two files, no then manually created
SSH-KEYGEN-T rsa-c [email protected] and then all the way back, in the main directory there is a. SSH directory with
Id_rsa is the private key, Id_ras_pub is the public key

2) Add the public key to the Sshkey in GitHub, the public key is used to differentiate the computer, the GitHub warehouse can be associated
More than one computer, the sshkey of each computer can be added to the remote push

3) associate a local library with a remote library
git remote add origin [email protected]:uolo/test.git
Origin is the name of the default remote library

4) Push local code to remote
Git push-u Origin Master
With the-u parameter, git not only pushes the local master branch to the remote new Master branch, but also associates the local master branch with the remote Master Branch, simplifying the command at a later push or pull.

The first time you use Git push or git clone to connect to GitHub, you get a warning that SSH validates GitHub
The key's fingerprint is from the GitHub server, enter Yes and then return to

6 cloning from a remote repository
git clone [email protected]:uolo/test.git from remote test library to local
Note that GitHub supports a variety of protocols, there are many cloned addresses, such as Https://github.com/uolo/test.git, GitHub supports HTTPS protocol, this kind of address is better to remember


Branch Management

...


Version fallback

...

Security risks

How to use Git

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.