1.新建代码库

1.1在当前目录新建一个 Git 代码库

$ git init
notion image

1.2新建一个目录,将其初始化为 Git 代码库

$ git init [project-name]
notion image

1.3下载一个项目和它的整个代码历史

$ git clone [url]
notion image

2.配置

Git 的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。

2.1设置提交代码时的用户信息

notion image

2.2编辑 Git 配置文件

$ vim ~/.gitconfig
notion image

2.3显示当前的 Git 配置

$ git config --list
notion image

2.4显示当前的 Git 配置

$ cat ~/.gitconfig
notion image

3.增加/删除文件

3.1添加指定文件到暂存区

$ git add [file1] [file2] ...
notion image

3.2添加指定目录到暂存区,包括子目录

$ git add [dir]
notion image

3.3添加当前目录的所有文件到暂存区

$ git add .

3.4删除工作区文件,并且将这次删除放入暂存区

3.4.1直接删除文件

$ git rm -fr 1.txt

3.4.2停止追踪指定文件,但该文件会保留在工作区

3.5改名文件,并且将这个改名放入暂存区

$ git mv [file-original] [file-renamed]
notion image

4.代码提交

4.1提交暂存区到仓库区

$ git commit -m [message]
notion image

4.2提交暂存区的指定文件到仓库区

$ git commit [file1] [file2] ... -m [message]
notion image

4.3提交工作区自上次 commit 之后的变化,直接到仓库区

$ git commit -a

4.4提交时显示所有 diff 信息

$ git commit -v
notion image

4.5使用一次新的 commit,替代上一次提交如果代码没有任何新变化,则用来改写上一次 commit 的提交信息

$ git commit --amend -m [message]

5.分支

5.1列出所有本地分支

$ git branch
notion image
表示当前分支

5.2列出所有远程分支

$ git branch -r
notion image

5.3列出所有本地分支和远程分支

$ git branch -a
notion image

5.4新建一个分支,但依然停留在当前分支

notion image

5.5新建一个分支,并切换到该分支

$ git checkout -b [branch]
notion image

5.6新建一个分支,指向指定 commit

5.6.1查看提交的日志

$ git log

5.6.2新建一个分支,指向指定 commit ID

notion image

5.7新建一个分支,与指定的本地分支建立追踪关系

notion image

5.7.1DevOps_RC,增加一次提交

notion image

5.7.2DevOps_RC,再增加一次提交

notion image

5.8切换到指定分支,并更新工作区

5.8.1切换到建立追踪的分支DevOps_RC1

notion image

5.8.2从建立追踪的分支拉取最新的变化

notion image

5.9新建一个分支,与指定的远程分支建立追踪关系

5.9.1将本地的所有分支及信息全部推送到远程仓库

说明:origin,查看是否配置origin;默认克隆时,自动建立;
notion image

5.9.2查看origin信息

5.9.3新建一个分支,与指定的远程分支建立追踪关系

notion image

5.9.4切换到建立追踪关系的新分支DevOps_RC2

5.9.5使用 git pull 更新本地分支(会拉取建立追踪关系的分支代码)

5.10合并指定分支到当前分支

notion image
提示文件存在冲突,冲突解决后再提交

5.10.1解决合并产生的冲突

按照冲突提示,修改冲突文件 Readme.md
$ vim Readme.md
notion image

5.10.2编辑修改解决冲突

5.10.2.1冲突提示

notion image

5.10.2.2冲突解决

notion image

5.10.2.3查看当前状态

此处我的处理措施是,把合并结果加入当前分支
notion image

5.11选择一个 commit,合并进当前分支

$ git cherry-pick [commit]

5.11.1选择别的分支(DevOps_RC1)的一个commit

$ git log
notion image

5.11.2切换到另一个项目合并一个提交至此分支的分支

5.11.2.1切换分支(DevOps_Dev)

5.11.2.2将选定的commit,合并进当前分支

notion image

5.11.2.3查看当前状态

$ git status
notion image

5.11.2.4解决冲突

notion image

5.12删除分支

$ git branch -d [branch-name]
notion image

5.13删除远程分支

5.13.1查看当前分支的详细信息

$ git branch -av
notion image

5.13.2删除远程分支

$ git push origin --delete DevOps_RC2
notion image
可以看到远程的此分支已经删除了
notion image
可以看到远程的此分支已经删除了
notion image

5.13.3 删除远程分支

$ git branch -dr remotes/origin/DevOps_RC1
notion image
$ git branch -av
notion image

6.标签

6.1列出所有 tag

$ git tag
notion image
$ git tag -l
DevOps_Dev_V1.0
Dev_V1.0
V0.3FixBug
$ git tag --list
DevOps_Dev_V1.0
Dev_V1.0
V0.3FixBug

6.2新建一个 tag 在当前 commit

$ git tag [tag]$ git tag V0.3FixBug

6.3新建一个 tag 在指定 commit

$ git tag [tag] [commit] -m [message]$ git tag Dev_V1.0 89d1d5b198a77deb9096e11b02aec7187221f384 -m "初始开发基线"

6.4新建一个tag在当前commit,提交信息为:文件内容

$ git tag DevOps_Dev_V1.0 -F ReleaseNotes.md

6.5查看 tag 信息

notion image

6.6提交指定 tag到远程仓库

$ git push [remote] [tag]
$ git push origin V0.3FixBug
notion image

6.7提交所有 tag

$ git push [remote] --tags
$ git push origin --tags
notion image

6.8基于某个tag创建分支并切换至该分支

$ git checkout -b [branch] [tag]
notion image

7.查看信息

7.1显示有变更的文件

$ git status

7.2显示当前分支的版本历史

$ git log

7.3显示 commit 历史,以及每次 commit 发生变更的文件

$ git log --stat

7.4显示某个文件的版本历史,包括文件改名

$ git log --follow [file]
$ git whatchanged [file]

7.5显示指定文件相关的每一次 diff

$ git log -p [file]
$ git log -p Readme.md

7.6显示指定文件是什么人在什么时间修改过

$ git blame [file]
$ git blame Readme.md
notion image

7.7显示暂存区和上一个 commit 的差异

$ git diff --cached []
$ git diff --cached 89d1d5b198a77deb9096e11b02aec7187221f384
notion image
  • *不写commitID 默认是最新的commit ID **
notion image

7.8显示工作区与当前分支最新 commit 之间的差异

$ git diff HEAD
notion image

7.9显示两个分支之间的差异

$ git diff [first-branch]...[second-branch]$ git diff DevOps_RC3 DevOps_Dev

7.10显示两次commit之间的差异

$ git diff 304db4dfdd ab794ed848
notion image

7.11显示某次提交的元数据和内容变化

$ git show [commit]
$ git show ab794ed848f8cbe0f4b1be065555f5e0b1124d5d
notion image

7.12显示某次提交发生变化的文件

$ git show --name-only [commit]
$ git show --name-only b54962a30
notion image

7.13显示某次提交时,某个文件的内容

$ git show [commit]:[filename]
$ git show 304db4dfdd:Readme.md
notion image

显示所有的提交

$ git reflog
notion image

8.远程同步

8.1拉取远程仓库的所有变动

$ git fetch [remote]$ git fetch origin
notion image

8.2显示所有远程仓库

$ git remote -v
notion image

8.3显示某个远程仓库的信息

$ git remote show [remote]$ git remote show origin

8.4增加一个新的远程仓库,并命名

$ git remote add [shortname] [url]
$ git remote add DevOps_MaTest http://192.168.145.89/devops/devops_matest.git
$ git remote -v

8.5取回远程仓库的变化,并与本地分支合并

$ git pull [remote] [branch]

8.6上传本地指定分支到远程仓库

$ git push [remote] [branch]

8.7强行推送当前分支到远程仓库,即使有冲突

$ git push [remote] --force

8.8推送所有分支到远程仓库

$ git push [remote] --all

9.撤销

9.1恢复暂存区的指定文件到工作区

$ git checkout [file]

9.2恢复某个 commit 的指定文件到工作区

$ git checkout [commit] [file]

9.3恢复上一个 commit 的所有文件到工作区

$ git checkout .

9.4重置暂存区的指定文件,与上一次 commit 保持一致,但工作区不变

$ git reset [file]

9.5重置暂存区与工作区,与上一次 commit 保持一致

$ git reset --hard

9.6重置当前分支的指针为指定 commit,同时重置暂存区,但工作区不变

$ git reset [commit]

9.7重置当前分支的 HEAD 为指定 commit,同时重置暂存区和工作区,与指定 commit 一致

$ git reset --hard [commit]

9.8重置当前 HEAD 为指定 commit,但保持暂存区和工作区不变

$ git reset --keep [commit]

9.9新建一个 commit,用来撤销指定 commit

9.10后者的所有变化都将被前者抵消,并且应用到当前分支

badge