Detailed explanation of git usage under windows system

[1] Create a Git repository

First enter the folder where the code needs to be managed (it is recommended to create an empty folder for the first time, and then create a new notepad file, but do not use the one that comes with Microsoft, because there are problems with its encoding format, it is recommended to use text editors such as notepad++ Of course, it is also a good choice to use a code editor to enter this directory to create files), we first enter the test folder.

Now right-click inside the current folder and select Git Bash Here to open the Git command window. In this way, the directory pointed to by the newly opened window is the current folder, which saves the trouble of switching folders and jumping paths.

If it is the first time for this computer to use Git, then we must first write user information to the Git configuration file:

git config --global user.name "example"
git config --global user.email "[email protected]"

 Note when copying:

         1. The shortcut key used for copying in Git is: Ctrl+Insert

         2. The shortcut key for pasting in Git is: Shift+Insert

 After the above steps are completed, enter the command in the Git command window:

git init

At this time, a hidden file .git will be generated in the current folder (as developers, we usually do not allow hidden files to be truly hidden), this file is the basic "repository" created by Git.

At this point, our Git repository is created.

A well-formed project should usually include a README.md file, which we can add manually, or create a README.md with the following command:

echo "README for the git example" > README.md

 Or enter the command in the Git command window:

touch README.md

The content in the quotation marks is the text after the file is generated. You can write a title like this, and the specific content will be modified by a text editor later. By specification, the filename of this file should be capitalized.

After the addition is complete, remember to also submit the file to the Git repository according to the steps of "Add -> Submit"

 

[2] Add files to the Git repository

The Git repository has been created, let's do a simple test now. Create a new notepad file test.txt (if you already have a notepad file, you don't need to create it again).

Then enter the following command in the Git command window:

git add test.txt

Nothing happens? That's right. Is there information output? Then you are doing it wrong, please check if the command is entered correctly, or if the file name is entered correctly, and if the above command contains two spaces.

If you need to add multiple files, you can enter the corresponding file name + extension later, and separate each file with a space.

In fact, there is another lazy way, which is this:

git add .

It is followed by a "period" in English, which can help you submit all the files in the current directory to the Git repository at one time, which is also the most common practice in actual development.

 

[3] Ignore and exclude the addition of files

 

Create a repository

2.1 Create the directory where the resource library 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 Chinese characters in the path hit.

 

2.2 Program this directory into a repository that GIt can manage through the git init command:

 

$ git init
Initialized empty Git repository in /Users/michael/learngit/.git/

! Note: Version control systems can only track changes to text files, such as txt files, web pages, and all program code. The version controller can tell you every change you make, but there is no way to track binary files such as images and videos, only the file size changes. The word format is also a binary file under Windows, so if we really want to use the version control system, we must write the file in plain text, and it is strongly recommended to use the standard UTF-8 encoding. And for editing text files, we recommend Notepad++, and remember to set the default encoding to UTF-8 without BOM.

 

2.3 Adding files to the repository

Step 1: We create a new text file in the learngit directory of our repository.

Step 2: Use the git add command to tell GIt to add the file to the repository

 

$ git add test.txt

 

After executing the command, there is no prompt message.

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

 

$ git commit -m "wrote a test file"

 

Description: Executing the above command will print prompt information such as:

 

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

 

 

git commit command: The input after -m is the description of this commit, you can enter any meaningful content, so that it is convenient to find the change record from the history record.

Also, we can add many files at the same time and commit them together, for example:

 

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

 Three, version rollback

 

3.1 Modify the file

原始文件中的内容为:this is my first  time to use Notepad++;

在原始文件中添加新的内容为:Add a new line。

3.2使用git status命令查看状态:

 

$ git status
On branch master
Changes 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.txt
no changes added to commit (use "git add" and/or "git commit -a")

 

执行git status命令,会打印提示消息。上述提示消息中告诉我们文件被修改,并没有提交。

3.3使用git diff命令查看详细修改内容;

 

$ git diff
diff --git a/test.txt b/test.txt
index 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使用git add test.txt 命令和git commit -m “add a new line.”提交修改;然后在文本中新添加内容“add a new line again”,以上述命令重新执行一边。
3.5使用git log查看修改记录。

 

$ git log
commit 4e8b0d0aaa685a83fb96fe52997e5af1e9e541ce
Author: bill <[email protected]>
Date:   Sat Jun 28 13:48:29 2014 +0800
    add a new line again
commit d4d025a1cffaa761a7b82f39551465f7610a82db
Author: bill <[email protected]>
Date:   Sat Jun 28 13:47:43 2014 +0800
    add a new line
commit 3b15333fdbb147f183a9d3013eadfafc9b05b127
Author: bill <[email protected]>
Date:   Sat Jun 28 13:18:15 2014 +0800
    wrote a test file

上述命令git log执行后会打印出具体日志信息。从上述信息中我们可以得到每次提交的记录,记录中包含提交的描述性信息例如“wrote a test file”,提交时间,提交人的具体信息等。而“commit 3b15333fdbb147f183a9d3013eadfafc9b05b127”则是我们每次提交的提交版本号,也称之提交的记录ID。

 

如果我们不需要提交人,提交时间等信息,我们也可以以一种更简洁的方式查看日志,只需要加上“--pretty=online”参数即可。

$ git log --pretty=oneline
4e8b0d0aaa685a83fb96fe52997e5af1e9e541ce add a new line again
d4d025a1cffaa761a7b82f39551465f7610a82db add a new line
3b15333fdbb147f183a9d3013eadfafc9b05b127 wrote a test file

3.6使用git reset命令回退版本

 

在工作当中,我们不可避免的使用回退版本,例如一个模块负责人提交了一部分代码,在项目负责人发编译发布版本前离开了工作岗位,项目负责人在编译发布版本的时候发现模块负责人工作失误造成项目无法编译通过,为了不耽误整个版本发布的工作,负责人不得不回退版本。

 

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

上述命令中Head在Git中的概念是一个指向你正在工作中的本地分支的指针(可以把HEAD想象为当前分支的别名),其所对应的分支本质是个指向commit对象的可变指针。截止到目前的学习中,在我们若干差提交后,我们已经有了一个指向最后一次提交的master分支,它在每次提交的时候都会自动向前移动。

我们使用git log --pretty=oneline查看:

 

$ git log --pretty=oneline
d4d025a1cffaa761a7b82f39551465f7610a82db add a new line
3b15333fdbb147f183a9d3013eadfafc9b05b127 wrote a test file

这个时候我们发现记录中已经少了一条,并且打开文件也会发现,最后修改的内容已经不见了。

 

还拿上面的例子来说,如果项目管理员发现编译不能通过的原因不是模块负责人的误操作引起的,而是有其他原因引起的,并且当前需要发布的版本中需要模块负责人的改动,但是版本已经回退了,能否再回退到回退前的版本呢?

答案是肯定的,我们只需要知道我们需要回退到的那个版本号commit id即可(例如当前的命令窗口没有关闭,我们可以轻轻滑动滚轴就可以看到之前的版本号),或者知道前面一部分也可以。

 

$ git reset --hard 4e8b0d0
HEAD is now at 4e8b0d0 add a new line again

读者可以使用git log进行查看,是否已经回退成功。
但是如果当前的命令窗口已经关闭了,我们无法在命令窗口中查看我们之前打印的版本号了怎么办?git也为我们提供了一个命令来记录我们每次执行的命令“git reflog”

$ git reflog
4e8b0d0 HEAD@{0}: reset: moving to 4e8b0d0
d4d025a HEAD@{1}: reset: moving to HEAD^
4e8b0d0 HEAD@{2}: commit: add a new line again
d4d025a HEAD@{3}: commit: add a new line
3b15333 HEAD@{4}: commit (initial): wrote a test file

上述提示信息中最前面的字符串即是我们需要的版本号。

 

        总结:我们在电脑中能够看到的目录,例如我们新疆an的文件夹learngit文件夹就是一个工作区,而隐藏目录.git是git的版本库,在这个版本中的index文件(stage)是一个很重要的文件,我们称之为暂存区,git为我们自动创建的第一个分之master,以及指向master的一个指针称作HEAD。我们之前添加文件的操作“git add”,实际上是把文件修改添加到暂存区。而提交操作“git commit”则是把暂存区的所有内容提交到当前分支。  因此我们如果要提交修改,在提交前我们应该执行git add命令,把修改的文件添加到暂存区。

四,撤销修改 git checkout

我们不能绝对的保证在日常的工作中不会出任何差错,如果我们在提交代码前发前发现有错误,但是我们没有执行了git add命令把修改的文件添加到了暂存区,那么能否撤销此次修改呢?

答案也是肯定的。

$ git checkout -- test.txt

执行上述命令,没有任何提示消息。

而且我们也可以使用命令git reset HEAD file把暂存区的修改撤掉(unstage),重新放回工作区。

git reset HEAD test.txt

五,删除文件

首先我们新键一个文件,并去删除它

如果我们没用把它提交到版本库我们可以在文件管理器手动删除或者使用命令"rm 文件名"的方式进行删除,而如果我们要删除版本库中的文件我们可以使用"git rm 文件名"的方式来操作。

$ git rm test1.txt
rm 'test1.txt'

六,远程仓库

我们知道Git是分布式版本控制系统,同一个Git仓库,可以分不到不同的机器上,怎么分布呢?最早,有一台机器又一个原始版本库,此后其他机器进行克隆原始的版本,而且每台机器的版本库版本是一样的,没有主次之分。而且我们可以在充当“服务器”的机器上进行克隆,也可在同一台机器上克隆多个版本库,只要不在同一个目录下就好。

6.1从“服务器”仓库克隆,使用github进行git存储

第一步:注册GItHub帐号,GitHub官网地址:https://github.com/ ,点击打开

第二步:创建SSH Key。在用户主目录下,如果有.ssh目录,并且该目录下有id_rsa和id_rsa.pub这两个文件,(跳过下一步操作),如果没有在windows打开Git Bash(linux 下打开Shell),创建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 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 [email protected]

回车,使用默认设置就好,无需设置密码。如果顺利在用户主目录下会看到id_rsa和id_rsa.pub两个文件,其中id_rsa是私钥,不可泄露出去,而id_rsa.pub是公钥,可以公开。

第三步:登陆GitHub,打开Account settings,SSH Keys页面,并点击Add SH Key,添加SSH Key,Title可以自由定义,Key文本框中就是id_rsa.pub文件的内容,直接复制即可。

如果点击Add  SSH Key无反应,也就是没有弹出Title和Key的编辑框可能是浏览器的问题,换一个浏览器试试,360浏览器6.3的版本(其他版本没试,整的我开始还以为为是被墙掉了,后来验证一下才发现是浏览器的问题)就会出现这样的失误。

这里还需要解释一下,GitHub需要SSH Key的原因是为了确认确实是由你来提交的,而不是他人。

第四步:在GitHub上点击Create a new repo按钮,创建一个新的仓库。

创建成功后的界面:

第五步,将本地资源库推送到远程仓库中。

由于我们本地已经存在了一个仓库,我们可以根据上面的提示将本地的仓库推送到远程仓库中去。(请注意用户名正确,你自己的用户名)

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

执行上述命令,没有任何信息提示;

接着执行

$ git push -u origin master
Username for 'https://github.com': huangyabin001
Password for 'https://[email protected]':
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 -> master
Branch master set up to track remote branch master from origin.

在输入的过程中会要求你输入你在GitHub上的用户名和密码。根据提示进行操作即可。远程克隆到本地就不再赘述了,按照上面的命令进行执行即可。

刷新GitHub我们就可以看到我们push的仓库中的内容了。

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326141524&siteId=291194637