In-depth learning: Getting Started with Windows git tutorial (top)

Source: Internet
Author: User
Tags first string version control system

One, install the git:1.1linux on the install command:

sudo apt-get install git

1.2 Install git on Windows:

Using the Windows version of the Msysgit, the official: http://msysgit.github.io/, click to enter the official website, if the official website is not normal download I here have the current version, has been uploaded to the CSDN, for:/http download.csdn.net/detail/huangyabin001/7564005, click to go to download

1.3 Setup is complete for configuration:

$ git config --global user.name "Your Name"$ git config --global user.email "[email protected]"
Because Git is a distributed version control system, each machine needs an identity, that is, your name and email address.

Second, create repository 2.1 to create the directory where the repository is located, command:

$ mkdir learngit$ cd learngit$ pwd/Users/michael/learngit
The PWD command is used to display the full path of the current directory, in order to avoid various problems we try to avoid path hits appearing in Chinese characters.

2.2 Use the GIT init command to program this directory to a repository that git can manage:

$ git initInitialized empty Git repository in /Users/michael/learngit/.git/
! Note: The version control system can only track changes to text files, such as txt files, Web pages, and all program code. The version controller can tell you each change, but the picture, the video and so on binary file cannot track, only knew the file size change. Word format is also a binary file under Windows, so if we want to really use the version control system, we write the file in plain text, and we strongly recommend using standard UTF-8 encoding. and edit the text file we recommend notepad++, and remember to set the default encoding to UTF-8 without BOM.


2.3 Adding files to the repository the first step: we create a new text file into our repository Learngit directory. Step two: Try the git add command to tell git to add the file to the repository

$ git add test.txt

After the command is executed, no message is prompted.

Step three: Use the command git commit to tell git to submit the file to the repository:

$ git commit -m "wrote a test file"

Note: Executing the above command will print a prompt message such as:

[master (root-commit) 3b15333] wrote a test file 1 file changed, 1 insertion(+) create mode 100644 test.txt

Git commit command:-M followed by the description of this submission, you can enter any meaningful content, so it is convenient to find changes in the history records.

In addition, we can add many files at the same time and submit them together, for example:

$ git add file1.txt$ git add file2.txt$ git add file3.txt$ git commit -m "add 3 files."
Third, version fallback

3.1 Modifying files

The contents of the original file are: This is my first, time to use notepad++;

Add a new line to the original file as: Add a new line.

3.2 Use the git status command to view the status:

$ git Statuson branch masterchanges not staged for commit: (use "git add <file> ..." To update what would be Committe d) (use "Git checkout-<file> ..." to discard changes in working D modified:test.txtno changes added to Commit (use "git add" and/or "Git Commit-a")

Executes the git status command, and a prompt message is printed. The above message tells us that the file has been modified and has not been submitted.

3.3 Use the git diff command to see the details of the changes;

$ git diffdiff--git a/test.txt b/test.txtindex d829b41. D6e3bba 100644---a/test.txt+++ b/test.txt@@-1 +1,2 @@-this is my first time to use notepad\ No newline at end of file+th Is was my first time to use Notepad+add a new line. \ No newline at end of file
3.4 Use git add test.txt command and git commit-m "add a new line." Submit the changes, and then add a new line again to the text with the above command to re-execute the side.
3.5 Use git log to view the modification record.
$ git logcommit 4e8b0d0aaa685a83fb96fe52997e5af1e9e541ceauthor:bill <[email protected]>date:sat June 28 13:48:29 +0800 Add a new line Againcommit D4d025a1cffaa761a7b82f39551465f7610a82dbauthor:bill <[email protected]>dat E:sat June 13:47:43 +0800 Add a new linecommit 3b15333fdbb147f183a9d3013eadfafc9b05b127author:bill <[emai L PROTECTED]>DATE:SAT June 13:18:15 +0800 wrote a test file
The above command will print out the specific log information after the Git log executes. From the above information we can get a record of each submission, the record contains descriptive information such as "wrote a test file", the time of submission, the author's specific information and so on. andCommit 3b15333fdbb147f183a9d3013eadfafc9b05b127"Is the submission version number that we submit each time, also called the record ID of the submission.

If we don't need information such as the author, submission time, etc., we can also view the log in a more concise way, simply by adding the "--pretty=online" parameter.

$ git log--pretty=oneline4e8b0d0aaa685a83fb96fe52997e5af1e9e541ce add a new line againd4d025a1cffaa761a7b82f39551465f7610a82db Add a new line3b15333fdbb147f183a9d3013eadfafc9b05b127 wrote a test file
3.6 fallback version with git reset command

In the work, we inevitably use the fallback version, for example, a module owner submitted a part of the code, before the project leader to compile the release version of the job, the project leader in compiling the release version of the module responsible for the failure of the project to compile, In order not to delay the release of the entire version of the work, the owner has to fallback version.

$ git reset--hard head^head is now in d4d025a add a new line
We use git log--pretty=oneline to view:

$ git log--pretty=onelined4d025a1cffaa761a7b82f39551465f7610a82db add a new line3b15333fdbb147f183a9d3013eadfafc9b05b127 wrote a test file
At this point we find that there is one missing in the record, and the open file will also find that the last modified content is missing.

Also take the above example, if the project administrator found that the compilation can not be caused by the wrong operation of the module owner, but for other reasons, and the current need to publish the version of the module owner's changes are required, but the version has been rolled back, can you fall back to the version before the fallback?

The answer is yes, we just need to know that the version number we want to fall back to is the commit ID (for example, if the current command window is not closed, we can simply swipe the roller to see the previous version number), or we know the previous part is possible.

$ git reset--hard 4e8b0d0head is now in 4e8b0d0 add a new line again
The reader can use git log to see if it has been rolled back successfully.
But what if the current command window is closed and we can't see the version number we printed earlier in the command window? Git also provides us with a command to record every command we execute "git reflog"

$ git reflog4e8b0d0 [email protected]{0}: reset:moving to 4e8b0d0d4d025a [e-Mail Protected]{1}: reset:moving to HEAD^4e8b 0d0 [Email protected]{2}: Commit:add a new line againd4d025a [email protected]{3}: Commit:add a new line3b15333 [email p ROTECTED]{4}: Commit (initial): wrote a test file
The first string in the preceding message is the version number we need.

Summary: We can see in the computer directory, such as our Xinjiang an folder Learngit folder is a work area, and the hidden directory. Git is a git repository, and in this version the index file (stage) is a very important file, which we call staging area, which git automatically creates for us and a pointer to master called head. We previously added a file to the operation "git add", which actually adds the file modification to staging area.  The commit operation "Git commit" commits all the contents of staging area to the current branch. So if we want to commit the changes, we should execute the git add command to add the modified file to staging area before committing.

Four, undo modify Git checkout

We cannot absolutely guarantee that we will not make any mistakes in our daily work, if we find errors before we send the code, but we have not executed the git add command to add the modified files to the staging area, can we undo this modification?

The answer is yes.

$ git checkout--test.txt
Execute the above command without any prompt message.

And we can also use the command git reset HEAD file to remove the staging area modification (unstage) and re-put it back into the workspace.
git reset HEAD test.txt
Five. deleting files

First we new key a file and go to delete it

If we don't use it to submit it to the repository we can delete it manually in the file Manager or use the command "RM file name", and if we want to delete the files in the repository we can use the "git RM file name" method to operate.

$ git rm test1.txtrm ' test1.txt '
VI, Remote Storage

We know that Git is a distributed version control system, the same Git repository can not be divided into different machines, how to distribute it? At the earliest, there was one machine and one original repository, after which other machines cloned the original version, and each machine's version of the repository was the same, with no primary or secondary points. And we can clone on a machine that acts as a "server" or clone multiple repositories on the same machine, as long as it's not in the same directory.

6.1 Cloning from the "server" repository, using GitHub for git storage The first step: Register your GitHub account, GitHub website address: https://github.com/, click Open. Step two: Create SSH Key. In the user home directory, if there is a. SSH directory, and the directory has Id_rsa and id_rsa.pub two files, (skip the next step), if not open git Bash in Windows (Linux Open shell), create SSH Key:
$ ssh-keygen-t rsa-c" [email protected] "generating public/private RSA key pair. Enter file in which to save the key (/C/USERS/STAR/.SSH/ID_RSA): Created directory '/c/users/star/.ssh '. Enter passphrase (empty for no passphrase): Enter same passphrase Again:your identification have been saved In/c/users/star /.ssh/id_rsa. Your public key has been saved in/c/users/star/.ssh/id_rsa.pub.the key fingerprint is:1e:49:1e:a4:fa:38:65:0e:4c:41:20: DF:67:A2:0C:BF [email protected] 

Enter, using the default settings, no need to set a password. If you can see the Id_rsa and id_rsa.pub two files in the user home directory, where Id_rsa is the private key, not leaked out, and Id_rsa.pub is the public key, can be public.

Step three: Login to GitHub, open the Account settings,ssh keys page, and click on Add SH Key, add SSH key,title can be freely defined, the Key text box is id_rsa.pub file content, directly copied.



If you click Add SSH Key No response, that is, no popup title and key edit box may be a browser problem, change a browser to try, 360 version of the browser 6.3 (other versions did not try, the whole I thought it was a wall fell, and later verified that it is a browser problem) will appear such a mistake.

It is also necessary to explain that the reason GitHub needs SSH key is to confirm that you are actually submitting it, not someone else.

Fourth step: Click on GitHub to create a new repository by clicking on the "Create a new Repo" button.


After creating the successful interface:


The fifth step is to push the local repository to the remote repository.

Since we already have a warehouse locally, we can push the local repository to the remote repository based on the above prompts. (Please note that the user name is correct, your own user name)

$ git Remote add Origin https://github.com/huangyabin001/learngit.git

Execute the above command without any information hint;

Then execute

$ GIT push-u origin masterusername for ' https://github.com ': Huangyabin001password for ' https://[email protected] ': Count ing objects:14, done. Delta compression using up to 4 threads.compressing objects:100% (9/9), done. Writing objects:100% (14/14), 1.15 KiB | 0 bytes/s, done. Total (Delta 2), reused 0 (Delta 0) to Https://github.com/huangyabin001/learngit.git * [New branch] master, MA Sterbranch Master set up to track remote branch master from Origin.

During the input process you will be asked to enter your username and password on GitHub. Follow the prompts to do so. Remote cloning to the local will not repeat, follow the above command to execute.


Refresh GitHub and we'll see what's in our push repository.

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.