Deep Learning: getting started with Git in Windows (I)

Source: Internet
Author: User

1. Install the command on Git: 1.1Linux:

Sudo apt-get install git

1.2 install Git on Windows:

Use Windows msysgit.

1.3 complete installation and configuration:

$ git config --global user.name "Your Name"$ git config --global user.email "email@example.com"
Because Git is a distributed version control system, each machine requires an identifier, that is, your name and Email address.

2. Create the directory where version library 2.1 is located and run the following command:

$ mkdir learngit$ cd learngit$ pwd/Users/michael/learngit
The pwd command is used to display the complete path of the current directory. To avoid various problems, we try to avoid hitting Chinese characters in the path.

2.2 Use the git init command to program this directory to a repository that can be managed by GIt:

$ 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, webpages, and all program code. The version controller can tell you every time you make changes, but images, videos, and other binary files cannot be tracked. You only know the size of the files. In Windows, word format is also a binary file, so if we really want to use version control systems, it is necessary to write files in plain text, and it is strongly recommended to use standard UTF-8 encoding. And edit the text file we recommend Notepad ++, and remember to set the default encoding for the UTF-8 without BOM.


2.3 Add a file to the resource library Step 1: Create a text file to the learngit directory of our resource library. Step 2: Use the git add command to tell GIt to add files to the resource library.

$ git add test.txt

No prompt is displayed after the command is executed.

Step 3: Run git commit to tell Git to submit the file to the repository:

$ git commit -m "wrote a test file"

Note: When you execute the preceding command, a prompt message is displayed, for example:

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

Git commit command:-m is followed by a description of the current submission. You can enter any meaningful content, so that you can easily find the change record from the history.

In addition, we can add many files and submit them together, for example:

$ git add file1.txt$ git add file2.txt$ git add file3.txt$ git commit -m "add 3 files."
Iii. Version rollback

3.1 modify files

The content in the original file is: this is my first time to use Notepad ++;

Add a new line to the original file.

3.2 run the git status Command to view the status:

$ git statusOn branch masterChanges not staged for commit:  (use "git add <file>..." to update what will be committed)  (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")

When you run the git status command, a message is displayed. The above message tells us that the file has been modified and has not been submitted.

3.3 run the git diff command to view the detailed modification content;

$ 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+this is my first time to use Notepad+Add a new line。\ No newline at end of file
3.4 run the git add test.txt command and git commit-m "add a new line. "Submit the changes; then add" add a new line again "in the text, and run the preceding command again.
3.5 use git log to view the modification records.
$ git logcommit 4e8b0d0aaa685a83fb96fe52997e5af1e9e541ceAuthor: bill After the above command git log is executed, it prints the specific log information. From the above information, we can obtain the record submitted each time. The record contains descriptive information such as "wrote a test file", the submission time, and the specific information of the submitter. "Commit 3b15333fdbb147f183a9d3013eadfafc9b05b127" is the version number of each submission, also called the submitted Record ID.

If you do not need the submitter, submission time, or other information, you can simply view the log 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 use the git reset command to roll back the version

During our work, we inevitably use the rollback version. For example, a module owner submits some code and leaves the job before the project owner releases the compilation and release version, when compiling and releasing a version, the project owner finds that the project cannot be compiled because of incorrect work of the module owner. In order not to delay the release of the entire version, the project owner has to roll back the version.

$ git reset --hard HEAD^HEAD is now at d4d025a add a new line

In the above command, the concept of Head in Git is a pointer to the local branch in your work (you can think of the HEAD as the alias of the current branch ), its corresponding branch is essentially a variable pointer to a commit object. As of now, after some of our poor commits, we have a master branch pointing to the last commit, which will automatically move forward each commit.

We use git log -- pretty = oneline to view:


$ git log --pretty=onelined4d025a1cffaa761a7b82f39551465f7610a82db add a new line3b15333fdbb147f183a9d3013eadfafc9b05b127 wrote a test file
At this time, we found that one record has been missing, and opening the file will also find that the last modified content has disappeared.

Take the preceding example as an example. If the project administrator finds that compilation fails because of incorrect operations by the module owner, the module owner must change the version to be released, but the version has been rolled back. Can I roll back to the previous version?

The answer is yes. We only need to know the version number commit id we need to roll back to (for example, the current command window is not closed, you can slide the scroll to see the previous version.) or you can know the previous version.

$ git reset --hard 4e8b0d0HEAD is now at 4e8b0d0 add a new line again
You can use git log to check whether the rollback is successful.
However, if the current command window is closed, we cannot view the version number we printed in the Command window. What should we do? Git also provides us with a command to record the "git reflog" command we run each time"

$ git reflog4e8b0d0 HEAD@{0}: reset: moving to 4e8b0d0d4d025a HEAD@{1}: reset: moving to HEAD^4e8b0d0 HEAD@{2}: commit: add a new line againd4d025a HEAD@{3}: commit: add a new line3b15333 HEAD@{4}: commit (initial): wrote a test file
The first character string in the above prompt is the required version number.

Summary: The directory we can see on the computer. For example, the learngit folder in Xinjiang is a work area, while the directory is hidden. git is the version library of git. In this version, the index file (stage) is a very important file, which is called a temporary storage zone. git is the first master that we automatically create, and a pointer to the master is called HEAD. The previous "git add" Operation for adding files actually adds modifications to the temporary storage zone. The commit operation "git commit" submits all the content in the temporary storage area to the current branch. Therefore, if you want to submit the changes, run the git add command before submission to add the modified files to the temporary storage area.

4. Undo and modify git checkout

We cannot guarantee that there will be no errors in our daily work. If an error occurs before the code is submitted, but we didn't execute the git add command to add the modified file to the temporary storage zone. Can we cancel this modification?

The answer is yes.

$ git checkout -- test.txt
No message is prompted when you execute the preceding command.

In addition, we can also use the command git reset HEAD file to remove the changes in the temporary storage area (unstage) and put them back into the workspace.
git reset HEAD test.txt
5. delete an object

First, press a new key and delete it.

If you have not submitted it to the version library, you can manually delete it in the file manager or delete it by running the "rm file name" command, if you want to delete files in the version library, you can use the "git rm file name" method.

$ git rm test1.txtrm 'test1.txt'
6. Remote Repository

We know that Git is a distributed version control system. The same Git repository cannot be divided into different machines. How can it be distributed? At the earliest, there was one machine and another original version library. After that, other machines cloned the original version, and each machine had the same version, with no primary or secondary knowledge. We can also clone multiple version libraries on the same server, as long as they are not in the same directory.

6.1 clone from the "server" repository, use github for git storage Step 1: Register GItHub account, GitHub official site address: https://github.com/, click to open. Step 2: Create an SSH Key. In the user's main directory, if there is. the ssh directory contains the files id_rsa and id_rsa.pub (skip the next step). If you do not open Git Bash in windows (Open Shell in linux), create an SSH Key:
$ ssh-keygen -t rsa -C "huangyabin001@163.com"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 has 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 huangyabin001@163.com

Press enter to use the default settings. You do not need to set a password. If the files id_rsa and id_rsa.pub are successfully viewed in the user's home directory, id_rsa is the private key and cannot be disclosed. id_rsa.pub is the public key and can be made public.

Step 3: log on to GitHub, open the Account settings and SSH Keys page, and click Add SH Key to Add an SSH Key. The Title can be freely defined. The Key text box contains the content of the id_rsa.pub file, copy directly.



If you click Add SSH Key, no response is returned, that is, the edit box of the Title and Key does not pop up. This may be a problem for the browser. Try another browser, version 360 of the browser (not for other versions, I thought it was because the wall was broken, And then I verified it and found it was a browser problem.

Here we also need to explain that the reason why GitHub requires an SSH Key is to confirm that it was submitted by you, not by others.

Step 4: Click Create a new repo on GitHub to Create a new repository.


Interface after successful creation:


Step 5: Push the local resource library to the remote repository.

Because a local warehouse already exists, we can push the local warehouse to the remote warehouse as prompted above. (Please note that the user name is correct, your own user name)

$ git remote add origin https://github.com/huangyabin001/learngit.git

No message is prompted when you execute the preceding command;

Then execute

$ git push -u origin masterUsername for 'https://github.com': huangyabin001Password for 'https://huangyabin001@github.com':Counting 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 14 (delta 2), reused 0 (delta 0)To https://github.com/huangyabin001/learngit.git * [new branch]      master -> masterBranch master set up to track remote branch master from origin.

During the input process, you are required to enter your username and password on GitHub. Follow the prompts to perform the operation. Remote cloning to the local machine will not go into details. simply execute the above command.


Refresh GitHub and we can see the content 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.