git大全
直接拉取远程分支的代码到本地远程跟踪分支(本地无需切换到对应分支)
git fetch origin op_list_server_V4.0D0045:op_list_server_V4.0D0045
配置config
git config --global user.name "Administrator"
git config --global user.email "root@example.com"
clone项目
git clone https://git.woa.com/group_path/project_path.git
创建并切换分支
git checkout –b test(分支名)
添加修改的文件到缓存区
git add 文件名
本地分支关联某个远程分支
git branch -u origin/op_server_V4.0D004
查看状态
git status
提交代码到本地仓库
git commit –m "描述信息"
查看历史
git log
拉取远程仓库最新代码
git pull origin test
推送代码到远程仓库
git push origin test
本地合并分支(合并前要切分支和拉最新代码)
git checkout master
git pull origin master
git merge test
变基rebase合并(rebase后还要回到主分支merge)
git checkout test
git rebase master
打tag,查看tag
git tag v1.0
git tag
git push origin v1.0
关联远程仓库
cd existing_folder
git init
git remote add origin url
git add .
git commit -m "init"
git push -u origin master
查看当前远程仓库地址
git remote –v
http与ssh互转
git remote set-url origin https://xxxxxxx.git
git remote set-url origin git@git.xxxxx.git
查看本地分支关联(跟踪)的远程分支之间的对应关系,本地分支对应哪个远程分支
[root@realserver git-test]# git branch -vv
git将某分支的某次提交合并到另一分支
切换到develop分支,敲 git log 命令,查找需要合并的commit记录,比如commitID:7fcb3defff。
切换到master分支,使用 git cherry-pick 7fcb3defff 命令,就把该条commit记录合并到了master分支,这只是在本地合并到了master分支
https://www.cnblogs.com/myitnews/p/12363154.html
git 拉取远程分支到本地
1、当本地有其他分支的代码仓库时
通过下述命令查看所有的远程分支:
git branch -r
下面有2种方法来拉取远程分支代码:
(1).需要本地分支和远程分支建立映射关系
git checkout -b 本地分支xxx origin/远程分支xxx
使用这种方式会在本地仓库新建本地分支xxx,并自动切换到新建的本地分支xxx,远程分支xxx的代码也拉取到了本地分支xxx中。采用这种方法建立的本地分支会和远程分支建立映射关系。
(2).不需要本地分支和远程分支建立映射关系
执行如下命令:
git fetch origin 远程分支xxx:本地分支xxx
使用这种方式会在本地仓库新建本地分支xxx,但是并不会自动切换到新建的本地分支xxx,需要手动checkout,当然了远程分支xxx的代码也拉取到了本地分支xxx中。采用这种方法建立的本地分支不会和远程分支建立映射关系。
2、当本地没有其他分支的代码仓库时
执行如下命令:
git clone -b 分支名 仓库地址
执行上述命令后就将远程分支拉取到了本地
查看帮助
git log --help
会打开一个网页。
关于路径
很多命令都会指定路径,比如git diff,我在文档里看到:
<path>…
The <paths> parameters, when given, are used to limit the diff to the named paths (you can give directory names and get diff for all files under them).
也就是说,指定了path的话,比如git diff,就只会看那个文件夹下面的文件。
不指定,一般是全部,或者当前路径。
git squash
https://www.cnblogs.com/hzglearn/p/13042282.html
git reset
将已经提交到index区域的,回退
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ git reset -p HEAD
diff --git a/readme.txt b/readme.txt
index 46d49bf..36bf35b 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a DISTRIBUYED version control system.
Git is free software.
Unstage this hunk [y,n,q,a,d,/,e,?]? y
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ 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 directory)
modified: readme.txt
soft模式回滚
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ git reset --soft 1c2e49781a3b7abc73b238ce9b880370fff01491
这个模式回滚后,只回滚已提交区域,回滚掉的代码,依然在index区域,并且保持:准备提交的状态。
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
此时,如果再进行commit,就会把这个提交掉,理论上,代码会和之前一模一样。
mixed模式回滚(默认不加参数时的模式)
该模式下,会把已提交区域和index区域,全部回退掉,但是工作区域不会变。
$ git log
commit a3858d4db808bcf439c24f09d4be0a93aca9b486
Author: cctvckl <914000408@qq.com>
Date: Sun Apr 26 09:06:59 2020 +0800
third
commit 1c2e49781a3b7abc73b238ce9b880370fff01491
Author: cctvckl <914000408@qq.com>
Date: Sun Apr 26 08:24:53 2020 +0800
readme file
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ git reset --mixed 1c2e49781a3b7abc73b238ce9b880370fff01491
Unstaged changes after reset:
M readme.txt
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
1 $ 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 directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ git log
commit 1c2e49781a3b7abc73b238ce9b880370fff01491
Author: cctvckl <914000408@qq.com>
Date: Sun Apr 26 08:24:53 2020 +0800
readme file
上面1处,可以看出来,我的工作区域没啥变化,但是index区域和已提交区域已经被回退了。
此时,如果比较index区域和已提交区域,是一模一样的。
$ git diff --cached readme.txt
hard模式回滚
git reset --hard 1c2e49781a3b7abc73b238ce9b880370fff01491
回滚index区域和已提交区域、以及工作区域。
回滚后,几个区域的东西全部一致,git diff都不会有任何结果。
工作区域的修改,会被丢弃。
git diff
比较工作区和index区
$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 36bf35b..9eef438 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,3 @@
Git is a DISTRIBUYED version control system.
Git is free software.
+kkk
比较index区和已提交区域
$ git diff --cached HEAD readme.txt
diff --git a/readme.txt b/readme.txt
index 46d49bf..36bf35b 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a DISTRIBUYED version control system.
Git is free software.
比较工作区和已提交区域
$ git diff HEAD readme.txt
diff --git a/readme.txt b/readme.txt
index 46d49bf..9eef438 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,3 @@
-Git is a version control system.
+Git is a DISTRIBUYED version control system.
Git is free software.
+kkk
查看任意两次提交之间的差异
$ git diff 1c2e49781a3b7abc73b238ce9b880370fff01491 65d0
diff --git a/readme.txt b/readme.txt
index 46d49bf..a9534b1 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1 @@
-Git is a version control system.
-Git is free software.
+iiii
哪个版本放前,哪个放后,结果不一样。
$ git diff 65d018e7067a6de74631c82c2c9bc17f3ff7f441 1c2e49781a3b7abc73b238ce9b880370fff01491
diff --git a/readme.txt b/readme.txt
index a9534b1..46d49bf 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
-iiii
+Git is a version control system.
+Git is free software.
如果只比较某一目录下的话,则如下:
// 当前目录
git diff HEAD 1c2e49781a3b7abc73b238ce9b880370fff01491 -- .
// sub目录
git diff HEAD 1c2e49781a3b7abc73b238ce9b880370fff01491 -- sub
只看某个文件夹下的诧异
$ git diff .
warning: LF will be replaced by CRLF in readme.txt.
The file will have its original line endings in your working directory.
diff --git a/readme.txt b/readme.txt
index a9534b1..0620a54 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1 @@
-iiii
+xxxxsss
diff --git a/sub/a.txt b/sub/a.txt
index e69de29..2b99771 100644
--- a/sub/a.txt
+++ b/sub/a.txt
@@ -0,0 +1 @@
+kkkk
warning: LF will be replaced by CRLF in sub/a.txt.
The file will have its original line endings in your working directory.
可以看到,当前目录下,有两个差异文件。一个是当前目录下的readme.txt,一个是sub目录下的。
如果只想查看sub目录下的:
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ git diff sub
diff --git a/sub/a.txt b/sub/a.txt
index e69de29..2b99771 100644
--- a/sub/a.txt
+++ b/sub/a.txt
@@ -0,0 +1 @@
+kkkk
warning: LF will be replaced by CRLF in sub/a.txt.
The file will have its original line endings in your working directory.
git checkout
丢弃工作区对某个文件的修改
$ 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 directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ git checkout -b MASTER readme.txt
fatal: Cannot update paths and switch to branch 'MASTER' at the same time.
Did you intend to checkout 'readme.txt' which can not be resolved as commit?
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ git checkout readme.txt
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean
从上面可以看到,不能和-b选项混用。官方说明如下:
git checkout [-p|--patch] [
] [--] … When
or --patchare given, git checkout does not switch branches. It updates the named paths in the working tree from the index file or from a named(most often a commit). In this case, the -band--trackoptions are meaningless and giving either of them results in an error.
也可以用来恢复,对某个文件的删除
$ rm -rf
.git/ readme.txt
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ rm -rf readme.txt
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ git checkout -- readme.txt
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean
丢弃工作区的修改,并使用某次提交的对应内容,来覆盖工作区
$ git checkout -p 1c2e49781a3b7abc73b238ce9b880370fff01491 -- readme.txt
diff --git b/readme.txt a/readme.txt
index a9534b1..46d49bf 100644
--- b/readme.txt
+++ a/readme.txt
@@ -1 +1,2 @@
-iiii
+Git is a version control system.
+Git is free software.
Apply this hunk to index and worktree [y,n,q,a,d,/,e,?]? y
执行上述之后,文件内容变为:
Git is a version control system.
Git is free software.
这个功能,有点类似于:svn里的,更新到指定版本。
git status
只查看某个目录下的状态
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: sub/a.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
Administrator@USER-20160930CJ MINGW64 /f/learngit (master)
$ git status sub/
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: sub/a.txt
常见错误处理办法
git pull更新错误解决办法
https://blog.csdn.net/xzongyuan/article/details/9212253
假设本地修改了,还没有add,也没有commit。要回退的话,可以这样:
参考了:
https://www.liaoxuefeng.com/wiki/896043488029600/897889638509536

浙公网安备 33010602011771号