博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Git-Notes
阅读量:7016 次
发布时间:2019-06-28

本文共 8191 字,大约阅读时间需要 27 分钟。

1.Git安装,直接在官网下载安装即可。

2.Git配置,使用config选项,配置名字和邮箱,如下所示

C:\Users\1yyg>git config --global user.name "zhumuxian"C:\Users\1yyg>git config --global user.email "495472831@qq.com"

3.创建仓库,使用init选项

F:\webim>git initInitialized empty Git repository in F:/webim/.git/

4.添加文件和提交,使用add和commit选项

F:\webim>git add index.js  //将文件添加到暂存区F:\webim>git commit -m "add index.js" //提交到master分支[master 92d277f] add index.js 1 file changed, 888 insertions(+) create mode 100644 index.js

5.查看仓库当前状态,使用status选项

F:\webim>git statusOn branch masterChanges not staged for commit:  (use "git add 
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: index.js

6.对比当前版本文件和上个版本文件的差异,使用diff选项

F:\webim>git diff index.jsdiff --git a/index.js b/index.jsindex 06d9b64..0c6787e 100644--- a/index.js+++ b/index.js@@ -19,6 +19,8 @@ $(function(){        var originDemension = {};++        var videoExtReg = /^\.(?=mpg|3gp|vob|rmvb|mov|flv|avi|wmv|mp4|mkv)/i,                musicExtReg = /^\.(?=flac|ape|wav|mp3|aac|ogg|wma)/i,                wordExtReg = /^\.(?=doc|docx)/i;

7.对于修改过的文件,再次使用add,commit命令提交即可

8.查看仓库提交日志,使用log选项,搭配--pretty=oneline更清晰

F:\webim>git log --pretty=oneline973ef9dcb5c4cc3de12fa4e3668a16ff3adb2992 add nihaoc8fbe43e78d3155262e427aa1df246156563cf23 add license.txtde0edd4c3fc56fdb74fd87ca9dfa0acb82847faa index.js92d277f2dbe720179530962b11d785c448c5296a add index.js5954ab28619985ab0ad768602578466dc92399fd del index.jscc4370f6424daf34c34f9b90196ab56857d61f47 add index.js

9.撤销版本,使用reset选项

git reset --hard HEAD^ //HEAD^后面必须跟一个空格,HEAD代表当前版本,HEAD^当前版本的上个版本,一次类推,也可以使用HEAD~[0-9..]来代替git reset --hard commit_id //使用commitId来撤销提交,commitId可以使用git log后者 git reflog来查看

10.查看所有命令,使用reflog选项

F:\webim>git reflog973ef9d HEAD@{
0}: reset: moving to HEAD^78ae302 HEAD@{
1}: reset: moving to HEAD78ae302 HEAD@{
2}: reset: moving to HEAD78ae302 HEAD@{
3}: commit: add heheda again973ef9d HEAD@{
4}: reset: moving to HEAD~19fb4130 HEAD@{
5}: commit: add heheda973ef9d HEAD@{
6}: commit: add nihaoc8fbe43 HEAD@{
7}: commit: add license.txtde0edd4 HEAD@{
8}: commit: index.js92d277f HEAD@{
9}: commit: add index.js5954ab2 HEAD@{
10}: commit: del index.jscc4370f HEAD@{
11}: commit (initial): add index.js

11.撤销修改

情况一:工作区中的内容修改了,但是没有添加到暂存区,可以利用checkout -- file来撤销改动F:\webim>git statusOn branch masterChanges not staged for commit:  (use "git add 
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: license.txtF:\webim>git checkout -- license.txtF:\webim>git statusOn branch master情况二:工作区的改动添加进了暂存区,但是没有提交,可以利用reset HEAD file来撤销修改,然后在使用git checkout -- file来恢复F:\webim>git reset HEAD license.txtUnstaged changes after reset:M license.txtF:\webim>git checkout -- license.txt

12.删除文件,使用rm选项,然后commit提交即可

F:\webim>git rm license.txtrm 'license.txt'F:\webim>git statusOn branch masterChanges to be committed:  (use "git reset HEAD 
..." to unstage) deleted: license.txtF:\webim>git commit -m "del license file"[master 5c258d3] del license file 1 file changed, 2 deletions(-) delete mode 100644 license.txt

 13.添加远程仓库

由于没有自己的git服务器,这里使用github的服务,首先创建ssh key,采用rsa加密,主要是github用来确认推送者的身份,如下所示

$ ssh-keygen -t rsa -C "495472831@qq.com"Generating public/private rsa key pair.Enter file in which to save the key (/c/Users/1yyg/.ssh/id_rsa):Created directory '/c/Users/1yyg/.ssh'.Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /c/Users/1yyg/.ssh/id_rsa.Your public key has been saved in /c/Users/1yyg/.ssh/id_rsa.pub.The key fingerprint is:SHA256:jDCsKX+gz2uoiLMYoBuphe7bK7KH4hTgsDkb3N6Gnks 495472831@qq.com

 然后将id_rsa.pub的内容复制到github网站个人设置里面的new ssh key中。接着使用git remote add添加远程库,该库必须在github中存在

F:\webim>git remote add webim https://github.com/zhu495472831/webim.git

接着在推送内容分支内容之前,使用git pull 先拉取最新的内容

F:\webim>git pull webim master//如果遇到fatal: refusing to merge unrelated histories错误,可以添加 --allow-unrelated-histories选项解决F:\webim>git pull webim master --allow-unrelated-histories

然后就可以推送内容了

F:\webim>git push webim masterCounting objects: 3, done.Delta compression using up to 4 threads.Compressing objects: 100% (3/3), done.Writing objects: 100% (3/3), 3.20 KiB | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To https://github.com/zhu495472831/webim.git   ef0c6db..1e4b6fa  master -> master

14.克隆仓库,使用git clone

F:\zmx\gittest>git clone git@github.com:zhu495472831/webim.gitCloning into 'webim'...The authenticity of host 'github.com (192.30.253.112)' can't be establRSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added 'github.com,192.30.253.112' (RSA) to the lin hosts.remote: Counting objects: 27, done.remote: Compressing objects: 100% (16/16), done.remote: Total 27 (delta 4), reused 23 (delta 3), pack-reused 0R  51% (14/27)Receiving objects: 100% (27/27), 12.45 KiB | 0 bytes/s, done.Resolving deltas: 100% (4/4), done.

15.分支,分支有利于多人协同作业

创建分支git branch name或者git checkout -b name创建并切换,查看分支git branch,切换分支git checkout branchName,删除分支git branch -d

F:\webim>git branch* masterF:\webim>git branch devF:\webim>git branch  dev* masterF:\webim>git checkout devSwitched to branch 'dev'F:\webim>git branch* dev  master

删除分支

F:\webim>git branch dev2F:\webim>git branch* dev  dev2  masterF:\webim>git branch -d dev2Deleted branch dev2 (was 1e4b6fa).F:\webim>git branch* dev  master

在创建分支之后,在分支上对文件进行的操作,别的分支感知不到,可以使用merge合并分支,便可以

F:\webim>git merge devUpdating 346ce30..d17e615Fast-forward hello.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)

 在合并分支的时候,可以指定--no-ff参数,表示禁用fast forward模式,这样在合并分支之后,删除分支时,分支的信息就不会丢失

F:\webim>git merge --no-ff -m "merge dev width no-ff" devMerge made by the 'recursive' strategy. hello.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)

16.存储现场,当在一个分支上进行开发的时,来了一个优先级更高的bug需要修复,这时不想提交未完成的代码,就可以使用stash命令来保存现场

F:\webim>git stash liststash@{
0}: On dev: hello.txtF:\webim>git stash popOn branch devChanges not staged for commit: (use "git add
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: hello.txt

git stash 保存     git stash save [message] 保存并备注       git stash list查看所有stash        git stash clear清空所有stash     git stash apply  stash@{n} 还原某个stash    git stash pop还原最近的一次stash

17 查看绑定的远程仓库信息

F:\webim>git remote -vwebim   https://github.com/zhu495472831/webim.git (fetch)webim   https://github.com/zhu495472831/webim.git (push)

18 创建和远程分支关联的分支

F:\webim>git checkout -b dev webim/devBranch dev set up to track remote branch dev from webim.Switched to a new branch 'dev'//也可以使用branch --track或--set-upstream-to关联F:\webim>git branch --set-upstream dev webim/devThe --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-toBranch dev set up to track remote branch dev from webim.

19 从远程库拉取内容

F:\webim>git pullAlready up-to-date.

20 打标签,使用tag命令

git tag:查看当前分支所有tag

git tag name:创建标签name,还可以指定commitid,例如git tag v1.0 63cbab46a467236a290fd4fb56439755f83ac49e,另外可以为标签加上描述,如git tag v1.0 -a -m "release 1.0"

 git show tag_name:查看某个标签的详细信息

F:\webim>git tag v1.0F:\webim>git tagv1.0F:\webim>git show v1.0commit 63cbab46a467236a290fd4fb56439755f83ac49eAuthor: zhumuxian <495472831@qq.com>Date:   Fri Oct 14 14:13:03 2016 +0800    add nizhidemadiff --git a/hello.txt b/hello.txtindex 8ef3fc0..09f6427 100644--- a/hello.txt+++ b/hello.txt@@ -1,2 +1,3 @@ hello world-this is a worla 
\ No newline at end of file+this is a worla
+
ֵ
\ No newline at end of file

 21 忽略文件,通过使用.gitignore文件配置,在git根目录新建一个名为.gitignore的文件,然后推送到github,内容如下所示

#images*.jpg*.png*.gif*.swf*.html

22 配置别名,使用git config alias命令配置命令的别名

使用命令配置如下

F:\webim>git config --global alias.st statusF:\webim>git stOn branch masterYour branch is up-to-date with 'webim/master'.nothing to commit, working tree clean

 

 

 

 

转载于:https://www.cnblogs.com/zmxmumu/p/5953940.html

你可能感兴趣的文章
VC调用javascript的几种方法
查看>>
Entity Framework简介
查看>>
图片轮播小列子
查看>>
趣文分享:有人将Android开发环境比作女人
查看>>
ASP.NET MVC 使用TryUpdateModel 更新的技巧
查看>>
构建最小根文件系统
查看>>
用法规则记录
查看>>
ESXi安装实录
查看>>
Leetcode: Roman to Integer
查看>>
Tomcat 配置加密的服务器连接器
查看>>
jQuery 学习笔记1 弹出一个对话框
查看>>
GCD介绍(二): 多核心的性能
查看>>
Openfire开发配置,Openfire源代码配置,OpenFire二次开发配置
查看>>
转 CentOS开启FTP及配置用户
查看>>
前端文摘:Web 开发模式演变历史和趋势
查看>>
win7的优化-1:隐藏我的电脑导航栏里的收藏等项目
查看>>
Consequence of Point-by-Point Bounds
查看>>
c# 封装的7zip压缩 (全源码,不含任何类库)
查看>>
三、OPENERP 中的对象关系类型
查看>>
PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
查看>>