廖雪峰大神git教程的学习笔记

git 学习日记

  • 学习廖雪峰大神git教程笔记和扩展
    • 命令汇总
    • git安装
    • 创建版本库(repository)
    • 时光机穿梭
      • 版本回退
      • 工作区和暂存区
      • 管理修改
      • 撤销修改
      • 删除文件
    • 远程仓库
      • 添加远程库
      • 从远程库克隆
    • 分支管理
      • 解决冲突
      • 分支管理策略
      • Bug 分支
      • Feature分支
      • 多人协作
    • 标签管理
      • 创建标签
      • 操作标签

学习廖雪峰大神git教程笔记和扩展

昨夜裙带解,今朝蟢子飞。
铅华不可弃,莫是藁砧归

命令汇总

设置name email
$ git config --grobal user.name "Your Name"
$ git config --grobal user.email "[email protected]"
查看name email
$ git config user.name
$ git config user.email
创建版本库(repository)
$ mkdir learngit 
$ cd learngit
$ pwd
$ git init
$ ls -ah
添加提交
$ git add readme.txt
$ git commit -m "wrote a readme file"
查看状态
$ git status
查看不同
$ git diff readme.txt
$ git diff HEAD -- readme.txt
版本回退
$ git log
$ git log --pretty=oneline
$ git reset --hard HEAD^
$ git reset --hard HEAD^^
$ git reset --hard HEAD~100
$ git reset --hard 版本号 1094a
记录命令
$ git reflog
撤回工作区修改
$ git checkout -- readme.txt
撤回暂存区修改
$ git reset HEAD readme.txt
$ git checkout -- readme.txt
撤回版本库修改
$ git reset --hard HEAD^
删除文件
$ git rm test.txt
$ git commit -m "remove test.txt"
撤回误删
$ git checkout -- test.txt

本地仓库和GitHub仓库关联,在本地仓库运行命令:
$ git remote add origin git@... ssh
$ git push -u origin master 
加了参数-u后,以后即可直接用git push 代替git push origin master
$ git push

从远程库克隆
$ git clone git@... ssh

创建分支
$ git checkout -b dev

$ git branch dev
$ git checkout dev

$ git switch -c dev

查看分支
$ git branch

合并分支
$ git merge dev
$ git merge --no-f -m "merge with no-f" dev
删除分支
$ git branch -d dev

查看分支合并情况
$ git log --graph --pretty=oneline --abbrev-commit
选项 说明
-p 按补丁格式显示每个更新之间的差异。
--stat 显示每次更新的文件修改统计信息。
--shortstat 只显示 --stat 中最后的行数修改添加移除统计。
--name-only 仅在提交信息后显示已修改的文件清单。
--name-status 显示新增、修改、删除的文件清单。
--abbrev-commit 仅显示 SHA-1 的前几个字符,而非所有的 40 个字符。
--relative-date 使用较短的相对时间显示(比如,“2 weeks ago”)。
--graph 显示 ASCII 图形表示的分支合并历史。
--pretty 使用其他格式显示历史提交信息。可用的选项包括 oneline,short,full,fuller 和 format(后跟指定格式)。

工作现场储存起来
$ git stash

恢复储存
恢复同时把stash删掉
$ git stash pop 
查询stash
$ git stash list
选择恢复到那个stash
$ git stash apply stash@{
     0}
删除stash
$ git stash drop

复制一个特定提交到当前分支
$ git cherry-pick 4c805e2

git安装

在Windows上安装

  1. 在git官网下载
  2. 在命令行输入
$ git config --global user.name " Your Name"
$ git config --global user.email "[email protected]"

我理解相当于自我介绍。
可以通过下面命令查询名字邮箱。

$ git config user.name
$ git config user.email

创建版本库(repository)

1.在一个合适地方,创建一个空目录:

$ mkdir learngit  
$ cd learngit
$ pwd

2.通过git init命令把目录变成git可以管理的仓库 。

$ git init
$ ls -ah

廖雪峰大神git教程的学习笔记_第1张图片
练习
在仓库创建一个readme.txt文件,内容如下

Git is a version control system.
Git is free software.

然后执行下面两个命令

$ git add readme.txt
$ git commit -m "wrote a readme file"

廖雪峰大神git教程的学习笔记_第2张图片

时光机穿梭

修改之前的readme.txt 文件

Git is a distributed version control system.
Git is free software.

运行 git status命令

$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   readme.txt

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

可以用git diff readme.txt命令查看文件不同地方

$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index d8036c1..013b5bc 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
 Git is free software.
\ No newline at end of file


下面我们提交修改

$ git add readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        modified:   readme.txt


86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git commit -m "add distributed"
[master b5d5e09] add distributed
 1 file changed, 1 insertion(+), 1 deletion(-)

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

版本回退

再一次修改readme.txt文件,内容如下

Git is a distributed version control system.
Git is free software distributed under the GPL.

然后提交

$ git add readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git commit -m "append GPL"
[master ebf8a18] append GPL
 1 file changed, 1 insertion(+), 1 deletion(-)

利用git log命令查看提交日志

$ git log
commit ebf8a18788a3fe2f157f9ebba3a9b44d51c600b9 (HEAD -> master)
Author: wangxilun0325 <[email protected]>
Date:   Mon Jun 1 23:21:21 2020 +0800

    append GPL

commit b5d5e09b5233ed861e3aaa384de810592c02278c
Author: wangxilun0325 <[email protected]>
Date:   Mon Jun 1 23:17:16 2020 +0800

    add distributed

commit d13b4771c0286ccea52530821cf1f833121e3568
Author: wangxilun0325 <[email protected]>
Date:   Mon Jun 1 23:06:13 2020 +0800

    wrote a readme.txt

利用git log --pretty=oneline命令简化日志

$ git log --pretty=oneline
ebf8a18788a3fe2f157f9ebba3a9b44d51c600b9 (HEAD -> master) append GPL
b5d5e09b5233ed861e3aaa384de810592c02278c add distributed
d13b4771c0286ccea52530821cf1f833121e3568 wrote a readme.txt

可以用git reset命令回退带append GPL版本

$ git reset --hard HEAD^
HEAD is now at b5d5e09 add distributed

廖雪峰大神git教程的学习笔记_第3张图片
我还可以通过git reset命令回到原来的版本,前提我们还记得原来的版本号。

$ git reset --hard ebf8
HEAD is now at ebf8a18 append GPL

我们可以通过git reflog命令记录每一个命令去找到版本号。

$ git reflog
ebf8a18 (HEAD -> master) HEAD@{
     0}: reset: moving to ebf8
b5d5e09 HEAD@{
     1}: reset: moving to HEAD^
ebf8a18 (HEAD -> master) HEAD@{
     2}: commit: append GPL
b5d5e09 HEAD@{
     3}: commit: add distributed
d13b477 HEAD@{
     4}: commit (initial): wrote a readme.txt

工作区和暂存区

工作区
我们创建的learngit文件夹
版本库
工作区的.git文件夹
版本库包含stage–>暂存区和git为我们创建的第一个分支master
廖雪峰大神git教程的学习笔记_第4张图片
练习
我们把readme.txt文件内容改为如下

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.

在工作区新增一个LICENSE文件

$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   readme.txt

Untracked files:
  (use "git add ..." to include in what will be committed)

        LICENSE.txt

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

我们用git add添加后,所有修改就到了暂存区。

$ git add readme.txt LICENSE.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        new file:   LICENSE.txt
        modified:   readme.txt

执行git commit命令,将暂存区的修改提交到分支。

$ git commit -m "understand how stage works"
[master 91811d0] understand how stage works
 2 files changed, 2 insertions(+), 1 deletion(-)
 create mode 100644 LICENSE.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

管理修改

1.我们修改readme.txt文件并添加。
2.我们再修改readme.txt文件不添加。
3.我们提交。
结论:提交只是把暂存区的修改提交到版本库。
练习
1.修改readme.txt内容

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes.

2.添加

$ git add readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        modified:   readme.txt

3.再次修改readme.txt内容

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.

4.不添加第二次修改,直接提交。

$ git commit -m "git tracks changes"
[master 91dfafb] git tracks changes
 1 file changed, 2 insertions(+), 1 deletion(-)

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   readme.txt

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

5.用git diff HEAD – readme.txt查看工作区和版本库中文件的差别。-- 之后有空格!!!

$ git diff head -- readme.txt
diff --git a/readme.txt b/readme.txt
index db28b2c..9a8b341 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,4 +1,4 @@
 Git is a distributed version control system.
 Git is free software distributed under the GPL.
 Git has a mutable index called stage.

撤销修改

1.修改了readme.txt,还没有add。
2.add了,还没有commit。
3已经commit,就需要之前的git reset --hard HEAD^。
练习
1.修改readme.txt

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
My stupid boss still prefers SVN.

2.没有add,直接撤回。

$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   readme.txt

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

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git checkout -- readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

3.修改readme.txt 并且add了。首先用git reset head readme.txt 命令把修改撤销到工作区,然后用git checkout – readme.txt 命令撤销工作区修改。

$ git add readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        modified:   readme.txt


86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git reset head readme.txt
Unstaged changes after reset:
M       readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   readme.txt

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

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git checkout -- readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean


删除文件

$ git add test.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git commit -m "add test.txt"
[master 8d2c7bd] add test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ rm test.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        deleted:    test.txt

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

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git rm test.txt
rm 'test.txt'

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git commit -m "remove test.txt"
[master f380b1c] remove test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 test.txt


如果删错了,恢复文件。

$ git checkout -- test.txt

远程仓库

1.创建SSH Key。

  1. 打开git bash。
  2. 使用cd ~/.ssh可以查看是否已配置SSH。
  3. 执行生成公钥和私钥的命令ssh-keygen -t rsa
$ cd ~/.ssh
bash: cd: /c/Users/86166/.ssh: No such file or directory

86166@LAPTOP-OC6T54S0 MINGW64 ~
$ ssh-keygen -t rsa -C "[email protected]"

2.登录GitHub,打开setting,SSH and GPG keys页面。然后添加ssh key

添加远程库

首先, 在GitHub上创建learngit仓库。
然后, 在本地learngit仓库运行命令。

$ git remote add origin [email protected]:wangxilun0325/learngit.git

最后, 将本地库所有内容推送到远程库上。

$ git push -u origin master

第一次推送master分支时,加上-u ,之后用git push origin master 就行。

从远程库克隆

上一节,我们是先有本地库和远程库,然后关联起来。
这一节,是先有远程库,然后从远程库克隆。
1.登录GitHub,创建一个新库,gitskills:
2.用git clone 命令克隆

$ git clone [email protected]:wangxilun0325/gitskills.git
$ cd gitskills

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/gitskills (master)
$ ls
README.md

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/gitskills (master)
$ cat ./readme.md
# gitskills

分支管理

$ git checkout -b dev
Switched to a new branch 'dev'

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git branch
* dev
  master

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git add readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git commit -m "branch test"
[dev 22ef2c9] branch test
 1 file changed, 2 insertions(+), 1 deletion(-)

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ cat ./readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes.
86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git merge dev
Updating f380b1c..22ef2c9
Fast-forward
 readme.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ cat ./readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes.
Creating a new branch is quick.
86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git branch -d dev
Deleted branch dev (was 22ef2c9).


也可以用 git switch -c dev 创建并切换
git switch master

解决冲突

  1. 创建feature1分支,在readme.txt做修改并提交。
  2. 在master分支,修改readme.txt并提交。
  3. 合并两分支
$ git switch -c feature1
Switched to a new branch 'feature1'

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (feature1)
$ git branch
* feature1
  master

$ git add readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (feature1)
$ git commit -m "AND simple"
[feature1 e1a3989] AND simple
 1 file changed, 1 insertion(+), 1 deletion(-)

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (feature1)
$ git switch master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git add readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git commit -m "& simple"
[master 9efde43] & simple
 1 file changed, 1 insertion(+), 1 deletion(-)

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git merge feature1
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master|MERGING)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add ..." to mark resolution)
        both modified:   readme.txt

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

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master|MERGING)
$ vim ./readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master|MERGING)
$ git add readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master|MERGING)
$ git commit -m "conflict fixed"
[master 94ddf9f] conflict fixed

遇到的问题
1.不识别switch命令,git版本太低了。升级之后解决。
2.vim规则不太记得了
可以去学习下
:wq dd yy
git log 查看合并情况

$ git log --graph --pretty=oneline --abbrev-commit
*   94ddf9f (HEAD -> master) conflict fixed
|\
| * e1a3989 (feature1) AND simple
* | 9efde43 & simple
|/
* 22ef2c9 branch test
* f380b1c (origin/master) remove test.txt
* 8d2c7bd add test.txt
* 91dfafb git tracks changes
* 91811d0 understand how stage works
* ebf8a18 append GPL
* b5d5e09 add distributed
* d13b477 wrote a readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git branch -d feature1
Deleted branch feature1 (was e1a3989).

分支管理策略

Fast forward 模式会丢掉分支信息。
实战一下–no-ff方式的git merge

  1. 创建dev分支,修改readme.txt内容并commit
  2. 切换master,–no-ff 合并dev
$ git switch -c dev
Switched to a new branch 'dev'

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git add readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git commit -m "add merge"
[dev c0a2df2] add merge
 1 file changed, 1 insertion(+), 1 deletion(-)

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git switch master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 4 commits.
  (use "git push" to publish your local commits)

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git merge --no-ff -m "merge with no-ff" dev
Merge made by the 'recursive' strategy.
 readme.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git log --graph --pretty=oneline --abbrev-commit
*   2deab3f (HEAD -> master) merge with no-ff
|\
| * c0a2df2 (dev) add merge
|/
*   94ddf9f conflict fixed
|\
| * e1a3989 AND simple
* | 9efde43 & simple
|/
* 22ef2c9 branch test
* f380b1c (origin/master) remove test.txt
* 8d2c7bd add test.txt
* 91dfafb git tracks changes
* 91811d0 understand how stage works
* ebf8a18 append GPL
* b5d5e09 add distributed
* d13b477 wrote a readme.txt

Bug 分支

创建dev分支,新建了hello.txt文件并add,修改了readme.txt,没有add。

$ git switch -c dev
Switched to a new branch 'dev'

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git add hello.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git status
On branch dev
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        new file:   hello.txt

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   readme.txt


用git stash 把工作状态储存起来。

$ git stash
Saved working directory and index state WIP on dev: 2deab3f merge with no-ff
$ git status
On branch dev
nothing to commit, working tree clean


我们回到master分支,创建新分支issue-101处理Bug。

$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 6 commits.
  (use "git push" to publish your local commits)

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git checkout -b issue-101
Switched to a new branch 'issue-101'

修复readme.txt,切回master分支合并issue-101分支。注意–no-ff

$ git add readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (issue-101)
$ git commit -m "fix bug 101"
[issue-101 2e8a278] fix bug 101
 1 file changed, 1 insertion(+), 1 deletion(-)

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (issue-101)
$ git switch master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 6 commits.
  (use "git push" to publish your local commits)

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (master)
$ git merge --no-ff -m "merged bug fix 101" issue-101
Merge made by the 'recursive' strategy.
 readme.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)


回到我们工作的dev分支,git stash list查看:

$ git switch dev
Switched to branch 'dev'

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git status
On branch dev
nothing to commit, working tree clean

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git stash list
stash@{
     0}: WIP on dev: 2deab3f merge with no-ff

两个办法:

  1. 用git stash apply 恢复,在用git stash drop删除。
  2. 用git stash pop 恢复同时把stash内容也删掉了。
$ git stash pop
On branch dev
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        new file:   hello.txt

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   readme.txt

Dropped refs/stash@{
     0} (cb4c58e58f467086454be3e34a6166d0e2a0eb09)

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git stash list

用cherry-pick命令将修复的bug操作同步到dev分支上。

$ git cherry-pick 2e8a278
error: your local changes would be overwritten by cherry-pick.
hint: commit your changes or stash them to proceed.
fatal: cherry-pick failed

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git add readme.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git commit -m "create hello.txt and add bug"
[dev f52223e] create hello.txt and add bug
 2 files changed, 1 insertion(+)
 create mode 100644 hello.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git cherry-pick 2e8a278
Auto-merging readme.txt
[dev 31c2f08] fix bug 101
 Date: Tue Jun 2 22:49:10 2020 +0800
 1 file changed, 1 insertion(+), 1 deletion(-)

Feature分支

每天加一个新功能,最好新建一个feature分支,在上面开发,合并,最后,删除feature分支。
没merge的分支强行删除-D

$ git switch -c feature-vuclan
Switched to a new branch 'feature-vuclan'

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (feature-vuclan)
$ git add vulcan.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (feature-vuclan)
$ git status
On branch feature-vuclan
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        new file:   vulcan.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (feature-vuclan)
$ git commit -m "add feature vulcan"
[feature-vuclan 4b68440] add feature vulcan
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 vulcan.txt

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (feature-vuclan)
$ git switch dev
Switched to branch 'dev'
86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)

$ git branch -d feature-vuclan
error: The branch 'feature-vuclan' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature-vuclan'.

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git branch -D feature-vuclan
Deleted branch feature-vuclan (was 4b68440).

多人协作

查看远程库

$ git remote
origin

86166@LAPTOP-OC6T54S0 MINGW64 /d/projects/learngit (dev)
$ git remote -v
origin  [email protected]:wangxilun0325/learngit.git (fetch)
origin  [email protected]:wangxilun0325/learngit.git (push)

推送本地分之

$ git push origin master

推送其他分支

$ git push origin dev

你的小伙伴要在dev上开发

$ git checkout -b dev origin/dev

你和你的小伙伴都在dev分支上对同样的文件修改了,他已经push了,这时你再push会报错。

$ cat env.txt
env

$ git add env.txt

$ git commit -m "add new env"
[dev 7bd91f1] add new env
 1 file changed, 1 insertion(+)
 create mode 100644 env.txt

$ git push origin dev
To github.com:michaelliao/learngit.git
 ! [rejected]        dev -> dev (non-fast-forward)
error: failed to push some refs to '[email protected]:michaelliao/learngit.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details

推送失败,因为你的小伙伴的最新提交和你试图推送的提交有冲突,解决办法也很简单,Git已经提示我们,先用git pull把最新的提交从origin/dev抓下来,然后,在本地合并,解决冲突,再推送

$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> dev

git pull也失败了,原因是没有指定本地dev分支与远程origin/dev分支的链接,根据提示,设置dev和origin/dev的链接:

$ git branch --set-upstream-to=origin/dev dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'

$ git pull
Auto-merging env.txt
CONFLICT (add/add): Merge conflict in env.txt
Automatic merge failed; fix conflicts and then commit the result.
$ git commit -m "fix env conflict"
[dev 57c53ab] fix env conflict

$ git push origin dev
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 621 bytes | 621.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To github.com:michaelliao/learngit.git
   7a5e5dd..57c53ab  dev -> de

标签管理

创建标签

$ git tag v1.0
$ git tag
$ git tag v0.8 f52c633  comnit id
$ git show v0.9
$ git tag -a v0.1 -m "version 0.1 released" 1094adb

操作标签

$ git tag -d v0.1
$ git push origin v1.0
$ git push origin --tags
$ git tag -d v0.9
$ git push origin :refs/tags/v0.9

你可能感兴趣的:(工具,git)