关于我与Git的故事,要从很久以前我第一次接触它开始说起。
刚加入开发团队的我与队友相互协作开发任务,常常感觉自己像个菜鸟。每天满心热情投入工作,却被代码提交出现的各种状况搞得焦头烂额,每次代码提交都像是一场冒险,小心翼翼又满心欢喜地将自己辛苦编写的代码push 到 remoteRepository,却还是可能遭遇代码冲突的“陷阱”。有时pull code时却发现代码被莫名其妙冲掉,自己的成果瞬间“消失”......
经常出错的我,时间久了被道友称为“Git杀手”。
Git作为日常开发必备工具,是码农必须掌握的技能,它是团队协作的桥梁,是保障代码有序管理的关键。
逃避解决不了问题,只有掌握它,才能在开发这条路上走得更远更稳。别害怕!接下来,就让我们一起揭开Git神秘的面纱,从基础学起,一步步成为Git高手,摆脱被它“支配”的日子,在开发之路上披荆斩棘。愿我们一起成长为“代码提交小能手”。
Git是什么
Git是一个分布式版本控制系统,用于跟踪代码的更改和协作开发。它允许开发人员在一个远程仓库(remoteRepository)中共享代码,并跟踪代码的更改历史。Git使用快照的方式保存代码的更改,使得代码的回滚和分支管理变得简单和高效。
Git与SVN的区别
Git和SVN都是版本控制系统,但它们在实现方式和功能上有一些区别:- Git是分布式版本控制系统,每个开发人员都可以在本地拥有一个完整的代码仓库,而SVN是集中式版本控制系统,所有代码都保存在中心服务器上。
一、新建代码库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| $ mkdir myproject $ cd myproject $ git init
$ git init myproject 这里做了一件事,就是在myproject根目录下创建一个.git子目录,用来保存版本信息。 $ ls .git
branches/ config description HEAD hooks/ info/ objects/ refs/
$ git clone [url]
$ git clone https://github.com/username/repo.git
|
二、配置
Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
1 2 3 4 5 6 7 8 9 10 11 12
| $ git config --list
$ git config -e [--global]
$ git config [--global] user.name "[name]" $ git config [--global] user.email "[email address]"
$ git config --global user.name "Your Name" $ git config --global user.email "youremail@domain.com"
|
三、增加/删除文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| $ git add [file1] [file2] ...
$ git add README.md
$ git add [dir]
$ git add src
$ git add .
$ git add -p
$ git rm [file1] [file2] ...
$ git rm file1.txt
$ git rm --cached [file]
$ git rm --cached file1.txt
$ git mv [file-original] [file-renamed]
$ git mv file1.txt file2.txt
|
四、代码提交
我们在暂存区保留本次变动的文件信息,修改完成就要把这些信息提交到历史,相当于生成了当前项目分支的一个快照(snapshot)。
此时Git就可以将项目恢复到任意一个快照,快照在Git里面有一个专门名词,叫做 commit,生成快照又称为完成一次提交。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| $ git commit [file1] [file2] ... -m [message]
$ git commit -m [message]
$ git commit -m "add file1.txt"
$ git commit -a -m [message]
$ git commit -am [message]
$ git commit --amend -m [message]
$ git commit --amend [file1] [file2] ...
|
五、分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| $ git branch
master dev
$ git branch -r
origin/master origin/dev
$ git branch -a
master dev * origin/master origin/dev
$ git branch [branch-name]
$ git branch dev
$ git checkout -b [branch]
$ git checkout -b dev
$ git branch [branch] [commit]
$ git branch dev 3d6f9c9f5e8a3f6e6f6e6f6e6f6e6f6e6f6e6f6
$ git branch --track [branch] [remote-branch]
$ git branch --track dev origin/dev
$ git checkout [branch-name]
$ git checkout dev
$ git checkout -
$ git branch --set-upstream [branch] [remote-branch]
$ git branch --set-upstream dev origin/dev
$ git merge [branch]
$ git merge dev
$ git cherry-pick [commit]
$ git cherry-pick 3d6f9c9f5e8a3f6e6f6e6f6e6f6e6f6e6f6e6f6
$ git branch -d [branch-name]
$ git branch -d dev
$ git push origin --delete [branch-name]
$ git push origin --delete dev
$ git branch -dr [remote/branch]
$ git branch -dr origin/dev
|
六、标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| $ git tag
v1.0 v2.0
$ git tag [tag]
$ git tag v1.0
$ git tag [tag] [commit]
$ git tag v1.0 3d6f9c9f5e8a3f6e6f6e6f6e6f6e6f6e6f6e6f6
$ git tag -d [tag]
$ git tag -d v1.0
$ git push origin :refs/tags/[tagName]
$ git push origin :refs/tags/v1.0
$ git show [tag]
$ git show v1.0
$ git push [remote] [tag]
$ git push origin v1.0
$ git push origin v1.0
$ git push [remote] --tags
$ git push origin --tags
$ git checkout -b [branch] [tag]
$ git checkout -b v1.0 v1.0
|
七、查看信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| $ git status
On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)
Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: test.txt
Changes not staged for commit: (use "git add <file>..." to update what will be committed)
$ git log
commit 3d6f9c9f5e8a3f6e6f6e6f6e6f6e6f6e6f6e6f6 Author: zhangsan <zhangsan@example.com> Date: Mon Oct 12 17:00:00 2020 +0800
test commit
$ git log --stat
$ git log -S [keyword]
$ git log [tag] HEAD --pretty=format:%s
$ git log [tag] HEAD --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --date=relative
$ git log --follow [file] $ git whatchanged [file]
$ git blame [file]
$ git diff
$ git diff --cached [file]
$ git diff HEAD
$ git diff [first-branch]...[second-branch]
$ git diff --shortstat "@{0 day ago}"
|
八、远程同步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| $ git fetch [remote]
$ git remote -v
$ git remote show [remote]
$ git remote add [shortname] [url]
$ git pull [remote] [branch]
$ git push [remote] [branch]
$ git push [remote] --force
$ git push [remote] --all
|
九、撤销
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| $ git checkout [file]
$ git checkout [commit] [file]
$ git checkout .
$ git checkout [file]@{v}
$ git checkout test.txt@{1}
$ git checkout [commit] [file]
$ git checkout 3d6f9c9f5e8a3f6e6f6e6f6e6f6e6f6e6f6e6f6 test.txt
$ git checkout [file]
$ git reset [file]
$ git reset
$ git reset --hard [commit]
$ git reset --hard HEAD^
$ git reset --hard HEAD^^
$ git reset --soft HEAD^
$ git reset --soft HEAD^^
$ git reset --mixed HEAD^
$ git reset --mixed HEAD^^
$ git reset [file]
$ git reset --hard
|
十、其他
1. 暂存区和工作区的区别(是Git版本控制中的两个核心概念)
工作区(Working Directory/Working Tree):
工作区是用户与代码直接交互的地方,即本地计算机上的项目文件夹。这是实际编辑文件、编写代码的区域。
工作区中的文件可以是从版本库中检出的文件,也可以是新创建的文件。对文件的修改首先发生在工作区中。
工作区的内容不会自动进入版本库,需要通过git add命令将更改添加到暂存区。
工作区中的文件可能有未跟踪(Untracked)、已修改(Modified)等状态。
暂存区(Staging Area/Index):
暂存区是一个临时的存储区域,用于保存将要提交的更改。可以理解为一个缓冲区,可以选择性地将工作区中的某些或全部更改放入暂存区,随后再提交到版本库。
暂存区记录的是工作区中已被标记为准备提交的更改。通过git add命令将文件或改动从工作区添加到暂存区。暂存区的内容是提交时的依据,只有暂存区中的内容才会被git commit提交到版本库。文件状态示例为已暂存(Staged)。
简而言之,工作区是用户进行文件编辑和修改的区域,而暂存区则是用于临时存放这些修改,并准备将其提交到版本库的区域。通过git add命令。可以将工作区的修改添加到暂存区;通过git commit命令,则可以将暂存区的修改提交到版本库中。
2. git reset 命令:在 Git 中用于将当前分支的 HEAD 指针移动到指定位置,并可以选择性地改变暂存区和工作区的内容。
git reset 有三种模式:--hard、--soft 和 --mixed(后者是默认模式,如果不指定模式则使用 --mixed)。下面是这三种模式的区别以及举例说明:
(1) git reset --hard
作用:将当前分支的 HEAD 指针移动到指定位置,并强制将暂存区和工作区的内容更新为与新的 HEAD 指针指向的提交完全一致。这意味着所有未提交的更改都会被丢弃。
举例说明:
假设我们有以下提交历史:
1 2 3 4
| commit 1 commit 2 commit 3 commit 4
|
现在我们执行 `git reset --hard HEAD~2`,这将把 HEAD 指针移动到 commit 2,并强制将暂存区和工作区的内容更新为与 commit 2 一致。commit 3 和 commit 4 的更改将被丢弃。
(2) git reset --soft
作用:将当前分支的 HEAD 指针移动到指定位置,并将暂存区的内容更新为与新的 HEAD 指针指向的提交一致,但工作区的内容保持不变。这意味着所有未提交的更改将保留在工作区中。
举例说明:
假设我们有以下提交历史:
1 2 3 4
| commit 1 commit 2 commit 3 commit 4
|
现在我们执行 `git reset --soft HEAD~2`,这将把 HEAD 指针移动到 commit 2,并将暂存区的内容更新为与 commit 2 一致。commit 3 和 commit 4 的更改将保留在工作区中。
(3) git reset --mixed
作用:将当前分支的 HEAD 指针移动到指定位置,并将暂存区的内容更新为与新的 HEAD 指针指向的提交一致,但工作区的内容保持不变。这意味着所有未提交的更改将保留在工作区中,并且暂存区的内容将与新的 HEAD 指针指向的提交一致。
举例说明:
假设我们有以下提交历史:
1 2 3 4
| commit 1 commit 2 commit 3 commit 4
|
现在我们执行 `git reset --mixed HEAD~2`,这将把 HEAD 指针移动到 commit 2,并将暂存区的内容更新为与 commit 2 一致。commit 3 和 commit 4 的更改将保留在工作区中,并且暂存区的内容将与 commit 2 一致。
总结
–hard:重置 HEAD、暂存区和工作区。需特别小心,因为它会丢弃所有未提交的更改。
–soft:仅重置 HEAD,保留暂存区和工作区。
–mixed(默认):重置 HEAD 和暂存区,保留工作区。
本篇只是简单介绍了Git的常见指令和场景,实际开发时请一定明确自己的Git操作指令和意图,否则可能会丢失代码,请谨慎使用!避免出现覆水难收的灾难!!!
Git练习传送门
__END__