git quick start


1. Basic use
1. Configure personal information

$ git config --global user.name "yourname"
$ git config --global user.eamil "youremail"

2. Create a repository

$ mkdir demo
$ cd demo

3.init repository

$ git init

After the warehouse is established, there is an additional .git folder in the warehouse, which stores some information of the recorded version library.

 

Add files to the repository.
Add a readme.md to the repository. For
example, use notepad ++ to create a new readme.md to write content at will.

$ git add readme.txt
$ git commit -m "create a readme file"
[master (root-commit) e6abfbe] create a readme file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme.md

-m is the description of this submission, which can be any meaningful content.

$ git add <filename>
$ git commit -m <massage>

Note: git can only track changes in text files, such as txt, web pages, all program codes, etc., and changes in files such as picture and video Microsoft word cannot be recognized. In addition, the text is encoded, it is strongly recommended to use UTF-8 encoding. (Don't use Microsoft's own notepad, don't use, don't use!)

 


If the version control now changes to a file, such as README.md, to
Git is a distributed version control system.

Enter the git status command:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md

no changes added to commit (use "git add" and/or "git commit -a")

The git status command allows us to keep track of the current status of the warehouse. The output of the above command tells us that README.md has been modified, but there are no changes to be submitted.
git diff README.md can view specific changes.
The git log can view the history record
commit 2ae605540dd4a194aa9c7b93ea8e26c664a3fa69 (HEAD-> master, origin / master). The next string of characters is the commit id, which means that each commit will have an id, which is used to distinguish each commit.

1. Version rollback In
git, HEAD is used to indicate the current version, which is the latest commit. The last version is HEAD ^, the last version is HEAD ^^, and the last 100 versions are HEAD ~ 100.
Git allows us to shuttle between versions of history, using the command git reset --hard commit_id.
If you regret it after going back, you need to find the target commit id in git relog.
Before the shuttle, use git log to view the commit history to determine which version to roll back to.
To return to the future, use git reflog to view the command history to determine which version to return to in the future.

2.
There is a .git file in the project working area of ​​the git temporary area . It is a version library, which stores a lot of things. The most important thing is the temporary area called stage, and the first branch automatically created by git master, and a pointer HEAD to the master.
When we add files to the repository, it is divided into two steps:
1. git add is actually to add file modifications to the temporary storage area.
2.git commit actually commits all the contents of the staging area to the current branch.

In addition, pay attention to a pit. When creating a new empty file, execute git add to add this file, and then add code to this file, and then execute git commit. This is that the repository will not commit the content added to the file to the branch. Why is this?
This is because git manages modifications. When the git add command is used, the first modification in the work area is put into the temporary storage area, ready for submission, but the second modification in the work area is not put in the temporary storage. Area, so git commit is only responsible for committing the changes in the staging area, that is, the first modification is submitted, the second modification will not be submitted.

How to submit the second amendment?

You can continue to git add and then git commit, or do n’t worry about committing the first modification, first git add the second modification, and then git commit, which is equivalent to merge the two modifications and submit them together:
first modification-> git add-> second modification-> git add-> git commit

3. Undo changes
If you make changes in a file, when you are ready to submit, you suddenly want to undo the changes at this time. Of course, you can undo the changes through the version rollback. If you modify one of the files, it will be very troublesome to roll back the version.

Here, you can use git checkout-file to discard the modification of
a file in the work area and undo all the changes in the work area. There are two cases here:
one is that this file has not been placed in the temporary file since the modification. The storage area, now, undo changes will return to the same state as the repository;
one is that this file has been added to the temporary storage area, and then modified, and now, the undo changes will return to the state after being added to the temporary storage area .
In short, this file is returned to the state of the last git commit or git add.
git checkout is actually to replace the version of the workspace with the version in the repository. Whether the workspace is modified or deleted, you can "one-click restore".

4.删除文件
git rm file删除版本库中的某文件。

二、为github账户设置ssh
1.创建SSH Key:

$ ssh-keygen -t rsa -C "[email protected]"

如果一切顺利的话,可以在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。

2.配置github ssh
登陆GitHub,打开“Account settings”,“SSH Keys”页面:
然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容,最后"Add Key"。

为什么GitHub需要SSH Key呢?因为GitHub需要识别出你推送的提交确实是你推送的,而不是别人冒充的,而Git支持SSH协议,所以,GitHub只要知道了你的公钥,就可以确认只有你自己才能推送。
当然,GitHub允许你添加多个Key。假定你有若干电脑,你一会儿在公司提交,一会儿在家里提交,只要把每台电脑的Key都添加到GitHub,就可以在每台电脑上往GitHub推送了。
最后友情提示,在GitHub上免费托管的Git仓库,任何人都可以看到喔(但只有你自己才能改)。所以,不要把敏感信息放进去。
如果你不想让别人看到Git库,有两个办法,一个是交点保护费,让GitHub把公开的仓库变成私有的,这样别人就看不见了(不可读更不可写)。另一个办法是自己动手,搭一个Git服务器,因为是你自己的Git服务器,所以别人也是看不见的。这个方法我们后面会讲到的,相当简单,公司内部开发必备。
确保你拥有一个GitHub账号后,我们就即将开始远程仓库的学习。

三、github项目的上传与下载
1.cd到项目根目录中,在命令行中输入

$ git init

把这个文件夹变成Git可管理的仓库

2.关联具体的代码仓库

$ git remote add origin xxxx

xxxx是ssh地址

3.将本地库推送到远程

$ git push -u origin master

如果远程仓库中包含本地不存在的文件(比如README.md),那么会报错

error: failed to push some refs to '[email protected]:xxx'

解决方案:

$ git pull --rebase origin master
$ git push -u origin master

4.从远程仓库克隆

$ git clone ssh_add

将远程仓库拷贝到本地。

四、分支管理
1.创建与合并分支
在版本回退里,你已经知道,每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支。截止到目前,只有一条时间线,在Git里,这个分支叫主分支,即master分支。HEAD严格来说不是指向提交,而是指向master,master才是指向提交的,所以,HEAD指向的就是当前分支。
一开始的时候,master分支是一条线,Git用master指向最新的提交,再用HEAD指向master,就能确定当前分支,以及当前分支的提交点:

每次提交,master分支都会向前移动一步,这样,随着你不断提交,master分支的线也越来越长。

当我们创建新的分支,例如dev时,Git新建了一个指针叫dev,指向master相同的提交,再把HEAD指向dev,就表示当前分支在dev上:

基本命令:

查看分支:git branch
创建分支:git branch <name>
切换分支:git checkout <name>或者git switch <name>
创建+切换分支:git checkout -b <name>或者git switch -c <name>
合并某分支到当前分支:git merge <name>
删除分支:git branch -d <name>

2.解决冲突
当Git无法自动合并分支时,就必须首先解决冲突。解决冲突后,再提交,合并完成。
解决冲突就是把Git合并失败的文件手动编辑为我们希望的内容,再提交。
用git log --graph命令可以看到分支合并图。

更新中…
参考:https://www.liaoxuefeng.com/wiki/896043488029600

如果出现报错

fatal: not a git repository (or any of the parent directories): .git

先git init

The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

在这里输入yes。

 

Guess you like

Origin www.cnblogs.com/LMIx/p/12677847.html