Git学习总结

Git学习笔记

一、Git本地仓库相关操作

  1. Linux环境的安装

sudo apt-get install git

  1. 指定机器用户和Email地址

git config --global user.name "用户名"
git config --global user.email "jinsefm@foxmail.com"

  1. 创建git仓库

cd /d/DATA/mkdir RepoLearn ##创建某个目录 git init ##初始化

  1. 添加文件到版本库

git add oneText.txt
warning: LF will be replaced by CRLF in oneText.txt. The file will have its original line endings in your working directory

出现警告:windows中换行符为CRLF,linux中换行符为LF,可禁用转换

git config --global core.autocrlf false

注:

  • core.autocrlf为true时,将add的所有文本各行末尾中的CRLF转为LF,本地checkout时转回CRLF。警惕此设置可能影响二进制文件的上传。
  • core.autocrlf为false时,line endings 不作修改,文本文件保持原样。
  • core.autocrlf为input时,git把add文件的CRLF全部转为LF,checkout时依旧为LF。
  1. 提交到版本库中git commit -m "更新消息"
$ git commit -m "add one file"
[master (root-commit) 453d52c] add one file 
  Committer: jinsefm <jinsefm@foxmail.com>
  Your name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly:    
	  git config --global user.name "Your Name"    
	  git config --global user.email you@example.com
  After doing this, you may fix the identity used for this commit with:    
	  git commit --amend --reset-author
  1 file changed, 1 insertion(+) 
  create mode 100644 learn.txt    
  1. 查看仓库当前状态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 directory)        
			modified:   learn.txt
no changes added to commit (use "git add" and/or "git commit -a")

提示文件已修改,使用以下命令查看修改内容,可以看出learn.txt增加了一行

  1. 对比工作区与暂存区的文件内容git diff learn.txt
$ git diff learn.txt
diff --git a/learn.txt b/learn.txt
index c073ff2..04454d6 100644
--- a/learn.txt
+++ b/learn.txt
@@ -1 +1,2 @@ 
 Git is a free software.
+Git is a distributed version control system.

再次提交前先add.

$ git add learn.txt
$ git commit -m "add one line"
[master 10ab3ff] add one line
 Committer: chen<chen@chen.net>
 Your name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly:    
	 git config --global user.name "Your Name"    
	 git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:    
		git commit --amend --reset-author 
1 file changed, 1 insertion(+)
$ git status
On branch master
nothing to commit, working tree clean
  1. 查看从最近到最远提交的历史记录 git log
$ git log
commit 10ab3ff547b3ddb89c5ddfd2ba134c6d443848dd (HEAD -> master)
Author: chen <chen@chen.net>
Date:   Tue Nov 13 15:19:30 2018 +0800    
	add one line
commit 453d52cfdcd4822d9cf7663843e1f23145a21504
Author: chen <chen@chen.net>
Date:   Tue Nov 13 14:55:58 2018 +0800    

add one file加–pretty=oneline一行显示

$ git log --pretty=oneline
10ab3ff547b3ddb89c5ddfd2ba134c6d443848dd (HEAD -> master)  add one line
453d52cfdcd4822d9cf7663843e1f23145a21504  add one file
  1. 版本回退

Git中,使用HEAD表示当前版本,上一版本记为 HEAD,上上版本记为HEAD^,往上100个版本记为HEAD~100

$ git reset --hard HEAD^   ##回退到上一个版本
HEAD is now at 453d52c add one file
$ cat learn.txt
Git is a free software.
$ git log
commit 453d52cfdcd4822d9cf7663843e1f23145a21504 (HEAD -> master)
Author: chen <chen@chen.net>
Date:   Tue Nov 13 14:55:58 2018 +0800    
		add one file

回到新版本,可用commitID代替,如下命令10ab3为最新版本的commitID的前五位(输多少位不限)

$ git reset --hard 10ab3
HEAD is now at 10ab3ff add one line
$ git reflog  ##查看命令的操作历史记录
10ab3ff (HEAD -> master) HEAD@{0}: reset: moving to 10ab3
453d52c HEAD@{1}: reset: moving to HEAD^
10ab3ff (HEAD -> master) HEAD@{2}: commit: add one line
453d52c HEAD@{3}: commit (initial): add one file
  1. 工作区与暂存区

工作区是.git目录所在的父目录,除了.git目录以外的区域。
版本库为.git目录,包含暂存区Stage和分支Master以及指向master的HEAD指针。

git add 是将工作区内的文件增加到暂存区中。
git commit是将暂存区的内容提交到当前分支。

某个文件按以下操作,那么只有第一次修改被提交了,第二次修改没有加到暂存区。
第一次修改–>git add–>第二次修改–>git commit

  1. 对比工作区文件与版本库里边某版本的区别

git diff HEAD[/^/~num]/版本id前几位 -- learn.txt

$ git diff HEAD~2 -- learn.txt
diff --git a/learn.txt b/learn.txt
index c073ff2..854d99f 100644
--- a/learn.txt
+++ b/learn.txt
@@ -1 +1,3 @@
-Git is a free software.
+Git is a free software distributed under the GPL.
+Git is a distributed version control system.
+Git has a mutable index called stage.
$ git diff 44c1 hello.py
diff --git a/hello.py b/hello.py
index c61e47e..2bbddbf 100644
--- a/hello.py
+++ b/hello.py
@@ -1 +1 @@
-print("HelloWorld!")
+print("HelloWorld,Man!")
  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:   learn.txt
no changes added to commit (use "git add" and/or "git commit -a")

git checkout -- learn.txt ##learn.txt 回到最近一次git commit或git add的状态

例:原状态–>第一次修改–>[git add]–>第二次修改–>[git checkout --file] --> [git reset HEAD file ##暂存区文件修改被丢弃]–>[git checkout --file]
第一次checkout:工作区file会恢复到第一次修改的状态
第二次checkout:工作区file会恢复到原状态

  1. 丢弃暂存区中某文件

git reset HEAD file

场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout – file。

场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD ,就回到了场景1,第二步按场景1操作。

场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退,不过前提是没有推送到远程库。

  1. 删除版本库中的某文件

git rm file

二、远程仓库

  1. 关联GitHub

<1>创建SSH Key。
在用户主目录下查看是否有.ssh的文件夹,其中是否包含id_rsa和id_rsa.pub文件。没有的话用以下目录创建:
ssh-keygen -t rsa -C "邮箱地址"
一路回车,使用默认值,中间有一步设置密码(Enter passphrase)也可.完毕后生成两份文件:id_rsa(私钥)和id_rsa.pub(公钥)

$ ssh-keygen -t rsa -C "jinsefm@foxmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/chen/.ssh/id_rsa):
Created directory '/c/Users/chen/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/chen/.ssh/id_rsa.
Your public key has been saved in /c/Users/chen/.ssh/id_rsa.pub.
The key fingerprint is:SHA256:xxx jinsefm@foxmail.com
The key's randomart image is:
+---[RSA 2048]----+
|.*+*o    .       |
|o Oo+ . . .    . |
|.o + o .   o  o  |
|o.. .    .. .. . |
|+o      S o..  +.|
|.*o        oo B o|
|+o=        . = o.|
|.E          o .o.|
|o.         . ....|
+----[SHA256]-----+ 

cat ~/.ssh/id_rsa.pub ##查看公钥内容

<2>Add SSH keys。
登录GitHub,点击个人头像,选择Settings进去,可以看到SSH and GPG keys。
点击New SSHkeys,复制公钥内容到Keys文本框中,title标题自定义即可。

<3>验证 SSH keys是否可正常连接GitHub服务器。
ssh -T git@github.com

第一次连接报以下信息,输入yes即可

The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:xxxx.
Are you sure you want to continue connecting (yes/no)?

之后连又有一个警告

Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.

需要将ip添加到host里边即可。window在 C:\Windows\System32\drivers\etc\hosts 文件中添加两行,linux 则 vim /etc/hosts,添加两行
13.250.177.223
52.74.223.119

  1. 本地仓库与远程仓库关联

在GitHub上创建一个仓库,输入仓库名,其他默认。

关联远程仓库
git remote add origin git@github.com:jinsefm/learnGit.git
git remote add origin git@server-name:path/repo-name.git

把本地仓库推送到远程仓库,第一次推送必须加上-u参数,表示不止推送master分支,还将本地的master分支与远程的master分支关联起来,后续推送可简化命令,不加参数。
git push -u origin master

注:在使用git命令时可能会报以下异常,这是当前路径不在本地仓库下,cd命令切换到本地仓库路径即可
fatal: not a git repository (or any of the parent directories): .git

  1. 克隆远程仓库 git clone git@github.com:jinsefm/HelloWorld.git

git clone git@server-name:path/repo-name.git ##只克隆了master分支

$ ls
AMOLED/  HelloWorld/  RepoLearn/
$ cd HelloWorld/
$ ls
README.md

Git支持多种协议的地址关联和获取远程仓库,一般使用ssh支持的原生git协议最快,使用https协议每次操作都需要输入口令。

三、分支管理

  1. 创建分支 dev

git branch dev

  1. 查看分支

git branch$ git branch dev* master

  1. 切换到分支dev

git checkout dev$ git branch* dev master

  1. 创建并切换到分支 dev

git checkout -b dev

  1. 把分支dev合并到当前分支

git checkout master ##切换到mater分支git merge dev ##合并dev到当前分支master

  1. 删除分支dev

git branch -d dev

  1. 解决冲突
$ pwd
/d/DATA/HelloWorld
$ ls
README.md
$ cat README.md# gitSkills
$ git checkout master
Switched to branch 'master'Your branch is up to date with 'origin/master'.
$ vim README.md
# gitSkills
create a new branch quick and simple
$ git add README.md
$ git commit -m "add new branch quickly"
[master a0c9169] add new branch quickly
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.  
	(use "git push" to publish your local commits)
nothing to commit, working tree clean
$ git checkout dev
Switched to branch 'dev'
$ vim README.md
# gitSkills
create a new branch!
$ git add README.md
$ git commit -m "create new branch"
[dev 0eea0da] create new branch
$ git checkout master
$ git merge dev
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
$ cat README.md
# gitSkills<<<<<<< HEAD
create a new branch quick and simple
=======
create a new branch!
>>>>>>> dev

如上,本地仓库master合并dev分支出现冲突。
最终确认冲突的内容,修改成如下并提交。

$ cat README.md
# gitSkills
create a new branch quickly and simple!
$ git add README.md
$ git commit -m "conflict fixed"
[master 033406c] conflict fixed

最后查看合并情况。

$ git log --graph --pretty=oneline --abbrev-commit
*   033406c (HEAD -> master) conflict fixed
|\
| * 0eea0da (dev) create new branch
* | a0c9169 add new branch quickly
|/
* b6f95c1 (origin/master, origin/HEAD) Initial commit

以上merge合并分支是fast forward的模式,如下删除分支后,对比上下历史记录中已看不到合并的分支信息。

$ git branch -d dev
Deleted branch dev (was 0eea0da).
$ git log --graph --pretty=oneline --abbrev-commit
*   033406c (HEAD -> master) conflict fixed
|\
| * 0eea0da create new branch
* | a0c9169 add new branch quickly
|/
* b6f95c1 (origin/master, origin/HEAD) Initial commit

合并分支时最好采用–on-ff模式的git merge,这种普通模式可以保留提交记录中的合并分支信息。

git merge --on-ff -m "merge information" dev

  1. 分支策略

在实际开发中,我们应该按照几个基本原则进行分支管理:
首先,master分支应该是非常稳定的,也就是仅用来发布新版本,平时不能在上面干活;

那在哪干活呢?干活都在dev分支上,也就是说,dev分支是不稳定的,到某个时候,比如1.0版本发布时,再把dev分支合并到master上,在master分支发布1.0版本;

你和你的小伙伴们每个人都在dev分支上干活,每个人都有自己的分支,时不时地往dev分支上合并就可以了。

  1. Bug分支处理

工作中dev分支的任务进行一半,突然接到紧急任务修复bug101,此时需要把dev的工作现场储藏起来,待bug解决后再恢复dev工作现场。

$ vim hello.py
HelloWorld!
$ ls
hello.py  README.md
$ git stash  ## 此时hello.py 未加到暂存区
No local changes to save
$ git add hello.py
$ git stash  ## 把git-add的保存起来
Saved working directory and index state WIP on master: 033406c conflict fixed
$ ls         ## 此时hello.py已经被储藏起来
README.md  
$ git stash list
stash@{0}: WIP on master: 033406c conflict fixed
##此时再去做bug修复的任务
$ git checkout master              ## 切换到主分支 master
$ git checkout -b issue-101        ## 在主分支上创建并切换到分支issue-101
$ vim bug101.py
$ git add bug101.py
$ git commit -m "fixed bug 101"    ## 提交修改到分支issue-101
$ git checkout master              
$ git merge --on-ff -m "merge bug fix 101" issue-101 ## 合并分支issue-101 到当前主分支master
$ git branch -d issue-101          ## 删除分支issue-101
$ git checkout dev                 ## 切换到dev分支
$ git status
On branch dev
nothing to commit, working tree clean
$ git stash list
stash@{0}: WIP on master: 033406c conflict fixed                   
$ git stash pop                 ## 恢复之前存储的进行一半的工作现场
On branch master
Your branch is ahead of 'origin/master' by 3 commits. 
	(use "git push" to publish your local commits)
Changes to be committed:  
	(use "git reset HEAD <file>..." to unstage)        
	new file:   hello.py
Dropped refs/stash@{0} (1dacb13726b35cc29c162d66f934418a72b397a3)  
## stash区的内容被删除,使用git stash apply命令则不删除
$ ls
hello.py  README.md          ## 此时hello.py已经恢复过来了

多次stash时,使用以下命令可以指定恢复哪个stash

git stash pop stash@{0} ## stash@{0} 对应stash list 中的第一个id

删除未合并的分支

git branch -D 分支名

  1. 多人协作

git remote -v ##查看远程库信息
git push origin master ##把本地分支master上所有提交推送到远程库origin
git checkout -b dev origin/dev ## 本地创建dev分支并同步远程dev分支到本地
git branch --set-upstream-to=origin/dev dev ##本地需已有dev分支,但未关联远程库dev分支,可用此命令关联,并未同步
git checkout dev
git pull ##从远程分支dev抓取最新的修改
git pull origin dev ##抓取远程库的dev分支到本地当前分支,并未将远程dev分支与本地当前分支关联

git-rebase -i <startpoint> [endpoint]
-i参数表示进入编辑本地提交的命令,编辑startpoint到endpoint的提交 如 startpoint可输80930f6或HEAD~6,endpoint 不输表示当前提交。

$ git log --oneline
7d5b8ee (HEAD -> dev, origin/dev) second edited develop.txt
1042ad0 merge edited develop.txt
c54232c who edited develop.txt
9ddd17c edited develop.txt
615a4c9 create branch& develop note80930f6 (origin/master, origin/HEAD, master) fix conlicts
$ git rebase -i 80930f6

按照以下commands文本的提示重新对历史提交进行操作,从80930f6的后一个commit开始到最后一个,前开后闭的区间形式。

pick 615a4c9 create branch& develop note
s    c54232c who edited develop.txt
s    9ddd17c edited develop.txt
pick 7d5b8ee second edited develop.txt

# Rebase 80930f6..7d5b8ee onto 80930f6 (4 commands)
#
# Commands:
# p, pick <commit> = use commit                   ##保留该commit
# r, reword <commit>=use commit, but edit the commit message##修改该commit的注释
# e, edit <commit> = use commit, but stop for amending    ## 执行到该commit时暂停
# s, squash <commit> = use commit, but meld into previous commit       ## 将该commit与上一个commit合并
# f, fixup <commit> = like "squash", but discard this commit's log message ## 除了s的功能,保留commit的注释
# x, exec <command> = run command (the rest of the line) using shell          ## 执行shell命令
# d, drop <commit> = remove commit                                                             ##丢弃该commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
## These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
#    However, if you remove everything, the rebase will be aborted.
#
#    
# Note that empty commits are commented out

该文件保存并退出后会依次顺序执行没有#开头的命令。若操作的commit中有冲突暂停需要先解决冲突再执行以下命令后继续进行
git rebase --continue
执行git rebase --abort则会全部停止,所有的commands都不会执行。
注:dev|REBASE-i 3/4表示执行到上述commands文本的第三个操作

<1>文本冲突直接abort的情况如下:

$ git rebase -i 80930f6
Auto-merging develop.txt
CONFLICT (content): Merge conflict in develop.txt
error: could not apply 9ddd17c... edited develop.txt
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 9ddd17c... edited develop.txt
chen@chen MINGW64 /d/DATA/HelloWorld2/HelloWorld (dev|REBASE-i 3/4)
$ cat develop.txt
This is  a  developnote!
<<<<<<< HEAD
The file is edited by  JINSEFM.
=======
The file was edited by someone.
>>>>>>> 9ddd17c... edited develop.txt
chen@chen MINGW64 /d/DATA/HelloWorld2/HelloWorld (dev|REBASE-i 3/4)
$ git rebase --abort
chen@chen MINGW64 /d/DATA/HelloWorld2/HelloWorld (dev)
$ cat develop.txt       
This is  a  developnote!
The file is edited by  JINSEFM.
edittime:2018-11-15

<2>解决包含Merge-Commit中的文本冲突的情况如下:

chen@chen MINGW64 /d/DATA/HelloWorld2/HelloWorld (dev)
$ git rebase -i 80930f6
Auto-merging develop.txt
CONFLICT (content): Merge conflict in develop.txt
error: could not apply 9ddd17c... edited develop.txt
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 9ddd17c... edited develop.txt
chen@chen MINGW64 /d/DATA/HelloWorld2/HelloWorld (dev|REBASE-i 3/4)
$ vim develop.txt
chen@chen MINGW64 /d/DATA/HelloWorld2/HelloWorld (dev|REBASE-i 3/4)
$ cat develop.txt
This is  a  developnote!
The file is edited by  JINSEFM.
chen@chen MINGW64 /d/DATA/HelloWorld2/HelloWorld (dev|REBASE-i 3/4)
$ git add develop.txt
# This is a combination of 2 commits.
# This is the 1st commit message:
create branch& develop note
# This is the commit message #2:

who edited develop.txt

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Thu Nov 15 11:03:07 2018 +0800# Committer: chen <chen@hz.net>
#
# interactive rebase in progress; onto 80930f6
# Last commands done (3 commands done):
#    squash c54232c who edited develop.txt
#    squash 9ddd17c edited develop.txt
# Next command to do (1 remaining command):
#    pick 7d5b8ee second edited develop.txt
# You are currently rebasing branch 'dev' on '80930f6'.
#
# Changes to be committed:
#    new file:   develop.txt
#
$ git rebase --continue  ##解决文本冲突后继续执行rebase
[detached HEAD 3397510] create branch& develop note 
	Date: Thu Nov 15 11:03:07 2018 +0800 
	Committer: chen <chen@hz.net>
Successfully rebased and updated refs/heads/dev.

查看log,发现中间两个commit已经被合并成一个了。

$ git log
commit e181daa611f5f212a2f626ef302d19dfaf387b75 (HEAD -> dev)
Author: chen <chen@hz.net>Date:   Fri Nov 16 11:00:48 2018 +0800   
	 second edited develop.txt
commit 339751018c5569352c5f32906b913a2c5038a5df
 Author: chen <chen@hz.net>Date:   Thu Nov 15 11:03:07 2018 +0800    
	 create branch& develop note    
	 who edited develop.txt
commit 80930f6ae70df682f8f6c6d422bc3198b44dff98 (origin/master, origin/HEAD, master)
Merge: 3c79c73 44c1d94
Author: chen <chen@hz.net>
Date:   Thu Nov 15 10:21:42 2018 +0800    
	fix conlicts
$ cat develop.txt
This is  a  developnote!
The file is edited by  JINSEFM.
edittime:2018-11-15

三、标签管理

  1. 添加标签

git tag v1.0 ## 给当前分支当前提交打上标签 v1.0
git tag v1.1 e181da ##给e181da版本加上标签v1.1
git tag -a v1.2 -m "realease 1.2 version" [commitID] ##给【commitID/当前版本】添加标签v1.2,同时指定标签信息为realease 1.2 version

  1. 查看所有标签

git tag

  1. 删除标签

git tag -d v1.0 ##删除标签v1.0
git push origin :refs/tags/v1.0 ##先删本地标签,再执行此命令删除远程标签v1.0

  1. 推送本地标签到远程库

git push origin v1.0 ## 把v1.0标签推送到远程
git push origin --tags ##推送全部尚未推送到远程的本地标签

四、参考资料传送门

廖雪峰老师的Git教程
Git官方文档
蚂蚁部落Git教程
git-cheat-sheet

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值