First of all, for Linus spent two weeks with C write git This behavior, Slag said this is the best interpretation of cattle X.
First, Git introduction
Git, is a distributed version control system, GitHub is to provide git warehouse hosting services. Distributed is relative to the centralized, first of all, the centralized version control system, the repository is centrally stored in the central server, and work, the use of their own computers, so the first from the central server to obtain the latest version, and then began to work, finished living, and then put their lives to the central server. Central server is like a library, you have to change a book, you must first borrow from the library, and then back home to change, and then put back to the library. Distributed version control system does not have a "central server", everyone's computer is a full version of the library, so that when you work, you do not need to network, because the repository is on your own computer. Since everyone has a full repository on their computers, how can multiple people collaborate? For example, you change the file a on your computer, your colleague also changed the file a on his computer, when you two just push their changes to each other, you can see each other's changes.
Second, git installation
Pass
Third, create the version library
1, first select a suitable place
2,$ git init changes this directory to a repository that git can manage
3, after each modification to first put the changes into the staged area
$git add/rm <file>
If it is a bulk modification, you can use git add-a
4. Tell git to submit the file to the repository
$ git commit-m "description of Change"
5,$ git Status View current warehouse status
Ps:git's command prompt is really excellent!!!
Four, Time Machine shuttle
$ git log view history, plus parameter--pretty=online can reduce output information. Such as:
$ git log--pretty=oneline3628164fb26d48395383f8f31179f24e0882e1e0 append Gplea34578d5496d7dd233c827ed32a8cd576c5ee85 Add distributedcb926e7ea50ad11b8f9e909c05226233bf755030 wrote a Readme File
3628164...882e1e0
is commit id
(version number), in Git, in the HEAD
current version, the previous version is, the last version HEAD^
is HEAD^^
, of course, 100 versions of the Write 100 is ^
relatively easy to count, so write HEAD~100
.
$ git reset--hard head^ fallback to previous version
$ git reset--hard 3628164 Specify the version number to rollback, the version number does not have to write the whole, write a similar line, Git will automatically go to find
$ git reflog used to record every command
Concepts of workspaces and staging area:
Workspace is the directory you can see
The repository is a hidden. Git directory
Git has a lot of stuff in the repository, the most important of which is the staging area called Stage(or index), and the first branch master
that git automatically creates for us, and master
a pointer to it called HEAD
.
Git add adds files to the file, actually adding files to the staging area;
The Git commit commits the change, essentially committing all the contents of the staging area to the current branch.
Because git automatically created the only master branch for us when we created the Git repository, now git commit is committing the changes to the Master branch.
$ git diff HEAD--<file> See the difference between the latest version of the workspace and the repository
$ git checkout--<file> Discard workspace Modifications
$ git reset HEAD <file> can undo staging area's changes
Five, remote warehouse
Add a remote repository on GitHub
1th step: Create SSH Key. In the user 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, if already have, you can skip to the next step. If not, open the shell (open git Bash under Windows) and create SSH Key:
$ ssh-keygen -t rsa -C "[email protected]"
You need to change the email address to your own email address, and then return to the default value, as this key is not used for military purposes, so there is no need to set a password.
If all goes well, you can find the directory in the user's home directory .ssh
, there are id_rsa
and id_rsa.pub
two files, these two are SSH key key pair, is the private key, can id_rsa
not be leaked out, id_rsa.pub
is the public key, it is safe to tell anyone.
2nd step: Login to GitHub, open "Account Settings", "SSH Keys" page:
Then, click "Add SSH Key", fill in any title, paste the contents of the file in the Key text box id_rsa.pub
:
Click "Add Key" and you should see the Key already added:
Why does GitHub need SSH key? Because GitHub needs to recognize that your push submission is actually pushed by you, not someone else impersonating it, and Git supports the SSH protocol, so if GitHub knows your public key, you can confirm that only you can push it.
Of course, GitHub allows you to add multiple keys. Assuming you have a number of computers, you can submit them at the company in a moment, and at home, just add each key to GitHub and push it to GitHub on every computer.
After creating a new warehouse on GitHub, push the contents of the local repository to the GitHub repository
$ git Remote add origin [email protected]:<user_name>/<repo_name>.git
In this way, a remote library called Origin is added, and the next step is to push all the contents of the local library to the remote library: 4
$ Git push-u Origin Master
Pushing the contents of the local library to the remote, with git push
the command, is actually pushing the current branch master
to the remote.
Since the remote library is empty, the first time we push master
a branch, with -u
parameters, Git will not only master
push the local branch content of the remote new master
branch, but also the local master
branch and the Remote master
Branch Association, You can simplify the command at a later push or pull. You only need to pass the command after associating:
$ GIT push origin master
Clone a local library from a remote library (SSH mode)
$ git clone [email protected]:<user_name>/<repo_name>.git
Getting Started with GitHub