My Git learning record------------Learning summary from Liaoche Teacher's website

Source: Internet
Author: User
Tags using git version control system

This article is used only for self-study, part of the reference from the Liaoche teacher's website


mkdir Path/file name (d:/test) Create folder
CD path/file name (CD d:/test) Enter folder
PWD shows the path of the current locationEnsure all-English path (note)


CD warehouse (repository) file path eg (CD d:/test)


Git init Initializes an empty repository, turning this directory into a repository that git can manage


Write a Readme.txt file that reads as follows:
Git is a version control system.
Git is free software. Put it in the Learngit directory.


Git add tells Git to add files to the repository
Git add readme.txt No display, that's right (Readme.txt must exist)
git Add. Add current all changes to next commit


Command git commit to tell git to commit the file to the repository
Git commit-m "wrote a Readme file"//Description what was submitted
git commit-m ' description ' eg (git commit-m ' my first text ')
Git commit command,-M is followed by the instructions for this submission,
Can enter any content, of course, the best is meaningful
Why does git add a file that requires add,commit two steps?
Because commit can commit many files at a time, you can add them multiple times to commit




Add to Git library after successful, manually to modify the TXT information
Use the $git status command to see the results, and he will tell you that the file has changed and has not been submitted.
The git status command allows us to keep track of the current state of the repository,
The above command tells us that Readme.txt has been modified,
But there are no changes ready to be submitted.


git diff readme.txt See what you've specifically modified
Then you can commit the modified $ git add readme.txt without prompting for instructions on success
Try the git status command before commit to see the Readme pending submission
Then submit $ git commit-m "fir modify"
Once submitted, we then use the git status command to see the current state of the repository.
You will see noting to commit


The following changes are repeated once, the contents are as follows:
Git is a distributed version control system.
Git is free software distributed under the GPL.


If you want to get the Readme back to the pre-modified version or to view the pre-modified version
Let's look at the history git log first.
The git log command displays the commit log from the most recent to the farthest
If too much output information, see dazzling,
Try the git log--pretty=oneline command to display only the description information
You see a bunch of similar 3628164 ... 882E1E0 is the commit ID (version number)


We're going to roll back the Readme.txt to the previous version.
First, git must know which version of the current version is, in Git, the current version with Head, that is, the latest commit
The previous version is head^, the last version is head^^, of course, 100 to write 100 ^ is more easy to count, so write a head~100
Now we fall back to the previous version $ git reset--hard head^
At this point we git log--pretty=oneline command
You will find that the latest version has not been seen, suddenly do not want to change, want to revert to the latest version of what to do
As long as the command window is not turned off, you can specify a future version according to the commit ID
For example, the first few of git reset--hard 283d9078//id don't write full
And then it's back to the latest version.


Now, you fall back to a version, turn off your computer, and regret it the next morning,
What if I want to revert to a new version? What if I can't find a new version of the commit ID?
In Git, there's always a regret pill to eat.
When you use git reset--hard head^ fallback to the Add distributed version,
If you want to revert to the append GPL, you must find the Append GPL's commit ID.
git provides a command git reflog to keep track of every command you make.
You'll be able to locate the Commitid according to the commit.






One of the differences between Git and other version control systems like SVN is the concept of staging area.
Workspaces (working directory) are directories you can see on your computer, like my Test folder is a workspace
Version Library (Repository)
The workspace has a hidden directory. Git, this isn't a workspace, it's a git repository.
Git's repository contains a lot of things, the most important of which is called the stage (or index) of the staging area,
There's the first branch master that git automatically created for us, and a pointer to master called head.


When we added the file to the Git repository, it was done in two steps:
The first step is to use git add to add the file, in fact, the file is modified to add to the staging area;
The second step is to commit the change with git commit, which in effect commits all of the staging area content to the current branch.
When we created the Git repository, Git automatically created the only master branch for us, so
Now, Git commit is to commit the changes to the Master branch.
So, git commit-m ' is putting staging area's stuff into the branch,
And git add is to submit the workspace changes to staging area
Pay attention to understanding this logic


Git is better designed than other version control systems because git tracks and manages changes, not files
That is, Git records the changes, not the log files.
$ cat Readme.txt Browse file contents focus on understanding the concept of staging area,
Mastering the area and role of add and commit operations


Undo Changes
Git checkout--file can be discarded workspace modification means that the Readme.txt file changes in the workspace are all revoked
One is that Readme.txt has not been put into the staging area since the modification, and now, the undo changes back to the same state as the repository;
One is that the Readme.txt has been added to the staging area and modified, and now the undo changes go back to the state that was added to the staging area.
All in all, let this file go back to the state of the last git commit or git Add.
Git checkout--in the file command--very important, not--becomes the "switch to another branch" command,
We will encounter the git checkout command again in the later branch management.


deleting files
In Git, the deletion is also a modification operation, we do the actual combat, first add a new file Test.txt to git and submit
$ git add test.txt
$ git commit-m "add test.txt"
Examples of git remote (the URL for a repository on GitHub) are as follows
git remote [email protected]:fenghuayangyi/hello-world.git
In general, you usually delete the unused files directly in the File Manager, or delete them with the RM command:
$ RM test.txt
At this point, git knows you deleted the file, so the workspace and repository are inconsistent, and the git status command will immediately tell you which files were deleted.
Now that you have two options, one is to remove the file from the repository, delete it with the command git rm and git commit:
$ git rm test.txt
The $ git commit-m "Remove test.txt" file was removed from the repository.


The other is wrong, because the repository is still there, so it is easy to restore the deleted files to the latest version:
$ git checkout--test.txt




Git is a distributed version control system, and the same git repository can be distributed across different machines.
It is certain that only one machine has an original repository, and since then, the other machine can "clone" the original repository, and each machine's repository is actually the same, and there is no primary and secondary points.
It is also possible to clone multiple repositories on a single computer, as long as they are not in the same directory.
This is often the case, find a computer to act as a server role, 24 hours a day to power on, everyone else from this "server" warehouse to clone a copy to their computer,
They also push their submissions to the server repository and pull others ' submissions from the server repository.
This allows for a team version control
GitHub is a magical site that provides a git repository hosting service, so you can get a git remote repository for free if you sign up for a GitHub account.


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 you already have one, you can jump right 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] ' email address for your own email address
First stop, hit enter, and then two times enter the password
You can see the. ssh folder in your Windows Home directory (the home directory is your C drive under the Users folder)
If all goes well, you can find the. SSH directory in the user's home directory with Id_rsa and id_rsa.pub two files
These two are key pairs of SSH key,
Id_rsa is the private key, cannot be leaked out,
Id_rsa.pub is a public key that can be safely told to anyone


2nd step: Login to GitHub, open "settings", "SSH Keys" page:
Fill in any title and paste the contents of the Id_rsa.pub file in your local. SSH directory in the key text box.


Why does GitHub need SSH key? Because GitHub needs to recognize that the submission you pushed is really something you pushed, not someone else posing,
And git supports the SSH protocol, so if you know your public key, GitHub can confirm that you're the only one to push.
GitHub allows you to add multiple keys. Suppose you have a number of computers that you submit in a moment at the company, and then submit at home,
As long as you add each key to GitHub, you can push it to GitHub on every single computer.
A git repository hosted on GitHub for free can be seen by anyone (but only you can change it). So don't put sensitive information in it.
If you don't want others to see the Git repository, there are two ways to do it, one for the intersection protection fee, and for GitHub to turn the public repository into private so that others can't see it (unreadable and not writable).
Another way is to do it yourself, take a git server, because it is your own git server, so others are invisible. This method we will talk about later, quite simple, the company internal development necessary.






To add a remote library
Once you have created a git repository locally, you want to create a git repository on GitHub and synchronize the two warehouses remotely so that the repository on GitHub can be used as a backup
and allow others to collaborate through the warehouse.
Log on to GitHub, then, in the top right corner, find the "Create a new Repo" button to create a new warehouse
In Repository name fill in the Test3, the other remains the default settings, click the "Create Repository" button, the successful creation of a new Git repository.
At the moment, this learngit warehouse on GitHub is still empty, and GitHub tells us that we can clone a new repository from this repository, or associate an existing local repository with it, and then push the contents of the local repository to the GitHub repository.
We run the command under the local Test3 repository, based on GitHub's prompt:


If you're updating to use HTTPS, the your URL might look like:
$ git Remote add Origin https://github.com/shlly/test3.git
If you're updating to use SSH, the your URL might look like:
or git remote add origin [email protected]:michaelliao/learngit.git


Examples of git remote add (the URL to the repository on GitHub) are as follows
git remote add [email protected]:fenghuayangyi/hello-world.git


Examples of git remote add origin (the URL of the corresponding repository on GitHub) are as follows
git remote add origin [email protected]:fenghuayangyi/hello-world.git


Please be careful to replace the above shlly with your own GitHub account name, otherwise, you are locally associated with my Remote library, the association is not a problem, but you later push is not pushed up,
Because your SSH key public key is not in my account list.


Once added, the remote library's name is origin, which is the default for Git, or it can be changed to another
Next, you can push all the contents of the local library to the remote library:
$ Git push-u Origin Master
Pushing the contents of the local library to the remote, using the git push command, is actually pushing the current branch master to the remote.
Since the remote library is empty, the first time we push the master branch, with the-u parameter, git will not only push the local master branch content to the remote new master branch,
It also associates the local master branch with the remote Master Branch and simplifies the command at a later push or pull.
After the push is successful, you can see in the GitHub page that the contents of the remote library are identical to the local


There will be failures in this process fatal unable to access I am the cause of the network, there is the use of HTTPS, my advice is to use SSH, the following is the explanation of the conversion


From now on, as long as the local commits, you can pass the command:
$ GIT push origin master




SSH warning
When you first connect to GitHub using Git's clone or push command, you get a warning:
The authenticity of host ' github.com (xx.xx.xx.xx) ' can ' t be established.
RSA key fingerprint is xx.xx.xx.xx.xx.
Is you sure want to continue connecting (yes/no)?
This is because Git uses SSH connection, and SSH connection is the first time to verify the GitHub server key, you need to confirm that GitHub key fingerprint information is really from the GitHub server, enter Yes.




Git proxy settings


See if setting


git config--global http.proxy


Set up Proxy


git config--global http.proxy 127.0.0.1:8582


Cancel Agent


git config--global--unset http.proxy


Using the command above, Git automatically sets a 127.0.0.1 agent after installation.




Switching remote URLs from SSH to HTTPS
$git remote-v
$git remote Set-url Origin Https://github.com/shlly/test3.git
$git remote-v


Switching remote URLs from HTTPS to SSH
$git remote-v
$git remote Set-url origin [email protected]:shlly/test3.git
$git remote-v




Git pull Origin Master (pulls down Origin master for the first time to configure native git)
This command is required when there are files in the synchronized library that require local synchronization on the line






ssh-t [email protected] uploading files to GitHub


Git pull Origin Master


Git push-u Origin Master


git Add.


Git commit-m ' 123 ' File description


Git push Origin Master






My Git learning record------------Learning summary from Liaoche Teacher's website

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.