文章目录
git常用命令
Git 基本命令介绍
创建本地仓库
git init
创建成功后,我们可以查看一下当前的一个状态,输入:
git status
如果已经成功配置为Git本地仓库,那么输入后可以看到:
On branch master
No commits yet
这表示我们还没有向仓库中提交任何内容,也就是一个空的状态。
基本的Linux命令
ls # 查看当前文件
ls -a # 查看所有文件,包括隐藏文件
ls -l # 查看文件可读可写
ls -al # 查看文件的类型,比如可读可写等
mkdir xxxx # 在同级目录中创建名为xxxx的文件夹
cd xxxx # 进入xxxx文件夹中
cd .. # 回到之前文件夹中
cd - # 回到上一次目录
cd . # 表示当前目录
ls .. # 获取上一级目录的所有文件
ls ../.. # 获取上一级目录的上一级目录
cat xxxx # xxxx为文件名,输出文件内容;例如 cat test.txt
ls
命令演示
zechaowei@zechaoweideMacBook-Pro git % ls
git.iml
zechaowei@zechaoweideMacBook-Pro git % ls -a
. .. .git .idea git.iml
zechaowei@zechaoweideMacBook-Pro git % ls -l
total 8
-rw-r--r-- 1 zechaowei staff 300 May 16 22:09 git.iml
zechaowei@zechaoweideMacBook-Pro git % ls -al
total 8
drwxr-xr-x 5 zechaowei staff 160 May 16 22:11 .
drwxr-xr-x@ 5 zechaowei staff 160 May 16 22:09 ..
drwxr-xr-x 10 zechaowei staff 320 May 16 22:11 .git
drwxr-xr-x 7 zechaowei staff 224 May 16 22:11 .idea
-rw-r--r-- 1 zechaowei staff 300 May 16 22:09 git.iml
cd
命令演示:
mkdir text
:创建名称为text的文件夹;
pwd
:显示了绝对路径;
cd text
:进入text文件夹;
cd ..
:返回上一级目录
zechaowei@zechaoweideMacBook-Pro git % mkdir text
zechaowei@zechaoweideMacBook-Pro git % ls
git.iml text
zechaowei@zechaoweideMacBook-Pro git % cd text
zechaowei@zechaoweideMacBook-Pro text % ls
zechaowei@zechaoweideMacBook-Pro text % pwd
/Users/zechaowei/Documents/003-Project/011-Git/011-Git | demo/git/text
zechaowei@zechaoweideMacBook-Pro text % cd ..
zechaowei@zechaoweideMacBook-Pro git % pwd
/Users/zechaowei/Documents/003-Project/011-Git/011-Git | demo/git
添加和提交
我们已经创建了text文件夹了,我们在text文件夹中创建一个test.txt文本文件;并且随便写一些东西在里面
接着我们来看看,如何使用git来管理我们文档的版本,我们创建一个文本文档,随便写入一点内容,接着输入:
git status
zechaowei@zechaoweideMacBook-Pro git % git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: text/test.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: text/test.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
.idea/
git.iml
其中Untracked files是未追踪文件的意思,也就是说,如果一个文件处于未追踪状态,那么git不会记录它的变化,始终将其当做一个新创建的文件,这里我们将其添加到暂存区,那么它会自动变为被追踪状态:
git add test.txt #也可以 add . 一次性添加目录下所有的
再次查看当前状态:
zechaowei@zechaoweideMacBook-Pro git % git add .
zechaowei@zechaoweideMacBook-Pro git % git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .idea/.gitignore
new file: .idea/misc.xml
new file: .idea/modules.xml
new file: .idea/vcs.xml
new file: git.iml
new file: text/test.txt
现在文件名称的颜色变成了绿色,并且是处于Changes to be committed下面,因此,我们的test.txt现在已经被添加到暂存区了。
接着我们来尝试将其提交到Git本地仓库中,注意需要输入提交的描述以便后续查看,比如你这次提交修改了或是新增了哪些内容:
git commit -m 'hello world'
zechaowei@zechaoweideMacBook-Pro git % git commit -m 'hello world'
[master (root-commit) fe32bc0] hello world
6 files changed, 37 insertions(+)
create mode 100644 .idea/.gitignore
create mode 100644 .idea/misc.xml
create mode 100644 .idea/modules.xml
create mode 100644 .idea/vcs.xml
create mode 100644 git.iml
create mode 100644 text/test.txt
接着我们可以查看我们的提交记录:
git log --all --graph --oneline
zechaowei@zechaoweideMacBook-Pro git % git log --all --graph --oneline
* fe32bc0 (HEAD -> master) hello world
我们还可以查看最近一次变更的详细内容:
git show [也可以加上commit ID查看指定的提交记录]
zechaowei@zechaoweideMacBook-Pro git % git show
commit fe32bc0da76409ce2a5fb7971abfdaf64ffb4143 (HEAD -> master)
Author: zechaowei <zechaowei2001@gmail.com>
Date: Tue May 16 22:29:18 2023 +0800
hello world
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..13566b8
--- /dev/null
+++ b/.idea/.gitignore
zechaowei@zechaoweideMacBook-Pro git % git show fe32 # 这里的fe32是第2行的代码的前几位数字,并不需要全部输入,只需要能够区分是哪一个提交即可
commit fe32bc0da76409ce2a5fb7971abfdaf64ffb4143 (HEAD -> master)
Author: zechaowei <zechaowei2001@gmail.com>
Date: Tue May 16 22:29:18 2023 +0800
hello world
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..13566b8
--- /dev/null
+++ b/.idea/.gitignore
再次查看当前状态,已经是清空状态了:
zechaowei@zechaoweideMacBook-Pro git % git status
On branch master
nothing to commit, working tree clean
回滚
当我们想要回退到过去的版本时,就可以执行回滚操作,执行后,可以将工作空间的内容恢复到指定提交的状态:
git reset --hard commitID
zechaowei@zechaoweideMacBook-Pro git % git commit -a -m 'Modify on master'
[master e885a98] Modify on master
1 file changed, 4 insertions(+), 1 deletion(-)
zechaowei@zechaoweideMacBook-Pro git % git log --all --graph --oneline
* e885a98 (HEAD -> master) Modify on master
* fe32bc0 hello world
zechaowei@zechaoweideMacBook-Pro git % git status
On branch master
nothing to commit, working tree clean
zechaowei@zechaoweideMacBook-Pro git % git reset --hard fe32
HEAD is now at fe32bc0 hello world
执行后,会直接重置为那个时候的状态。再次查看提交日志,我们发现之后的日志全部消失了。
zechaowei@zechaoweideMacBook-Pro git % git log --all --graph --oneline
* fe32bc0 (HEAD -> master) hello world
那么要是现在我又想回去呢?我们可以通过查看所有分支的所有操作记录:
git reflog
这样就能找到之前的commitID,再次重置即可。
zechaowei@zechaoweideMacBook-Pro git % git reset fe32
zechaowei@zechaoweideMacBook-Pro git % git log --all --graph --oneline
* fe32bc0 (HEAD -> master) hello world
zechaowei@zechaoweideMacBook-Pro git % git reflog
fe32bc0 (HEAD -> master) HEAD@{0}: reset: moving to fe32
e885a98 HEAD@{1}: reset: moving to e885a
fe32bc0 (HEAD -> master) HEAD@{2}: reset: moving to fe32
e885a98 HEAD@{3}: commit: Modify on master
fe32bc0 (HEAD -> master) HEAD@{4}: commit (initial): hello world
zechaowei@zechaoweideMacBook-Pro git % git reset e885a98
Unstaged changes after reset:
M text/test.txt
zechaowei@zechaoweideMacBook-Pro git % git log --all --graph --oneline
* e885a98 (HEAD -> master) Modify on master
* fe32bc0 hello world
Git分支
创建分支
通过下面命令可以查看当前分支:
git branch
我们发现,默认情况下是有一个master分支的,并且我们使用的也是master分支,一般情况下master分支都是正式版本的更新,而其他分支一般是开发中才频繁更新的。我们接着来基于当前分支创建一个新的分支:
git branch test # 创建test分支
zechaowei@zechaoweideMacBook-Pro git % git branch
* master
zechaowei@zechaoweideMacBook-Pro git % git branch test
zechaowei@zechaoweideMacBook-Pro git % git branch
* master
test
除了创建一个新的分支,当然也需要知道如何删除这个分支:
git branch -d xxxx # xxxx为分支名称
zechaowei@zechaoweideMacBook-Pro git % git branch -d test
Deleted branch test (was e885a98).
zechaowei@zechaoweideMacBook-Pro git % git branch
* master
现在我们修改一下文件,提交,再查看一下提交日志:
git commit -a -m 'branch master commit'
zechaowei@zechaoweideMacBook-Pro git % git commit -a -m 'branch master commit'
[master 62b4452] branch master commit
1 file changed, 2 insertions(+), 1 deletion(-)
zechaowei@zechaoweideMacBook-Pro git % git log --all --graph --oneline
* 62b4452 (HEAD -> master) branch master commit
* 843d723 (test) branch master commit
* e885a98 Modify on master
* fe32bc0 hello world
zechaowei@zechaoweideMacBook-Pro git % git checkout test
Switched to branch 'test'
zechaowei@zechaoweideMacBook-Pro git % git branch
master
* test
我们切换到其他分支后,通过添加-a来自动将未放入暂存区的已修改文件放入暂存区并执行提交操作。查看日志,我们发现现在我们的提交只生效于master分支,而新创建的分支并没有发生修改。(着重看test.txt
文本文件中的内容变化)
git checkout test
zechaowei@zechaoweideMacBook-Pro git % git branch
* master
test
zechaowei@zechaoweideMacBook-Pro git % git checkout test
Switched to branch 'test'
zechaowei@zechaoweideMacBook-Pro git % git branch
master
* test
合并分支
我们也可以将两个分支更新的内容最终合并到同一个分支上,我们先切换回主分支:
git checkout master
zechaowei@zechaoweideMacBook-Pro git % git commit -a -m 'branch test commit'
[test e3bb93e] branch test commit
1 file changed, 2 insertions(+), 1 deletion(-)
zechaowei@zechaoweideMacBook-Pro git % git checkout master
Switched to branch 'master'
zechaowei@zechaoweideMacBook-Pro git % git merge test
Auto-merging text/test.txt
CONFLICT (content): Merge conflict in text/test.txt
Automatic merge failed; fix conflicts and then commit the result.
接着使用分支合并命令:
git merge test
现在我们已经合并了分支了(合并分支失败),但是得到了如下提示
Auto-merging text/test.txt
CONFLICT (content): Merge conflict in text/test.txt
Automatic merge failed; fix conflicts and then commit the result.
在合并过程中产生了冲突,因为两个分支都对hello.txt文件进行了修改,那么现在要合并在一起,到底保留谁的hello文件呢?
我们可以查看一下是哪里发生了冲突:
git diff
zechaowei@zechaoweideMacBook-Pro git % git diff
diff --cc text/test.txt
index eadefad,7e77ff0..0000000
--- a/text/test.txt
+++ b/text/test.txt
@@@ -1,3 -1,3 +1,7 @@@
Hello World!
Hello C语言!
- Hello Python!
-Hello Java!
++<<<<<<< HEAD
++Hello Python!
因此,现在我们将master分支的版本回退到修改hello.txt之前或是直接修改为最新版本的内容,这样就不会有冲突了,接着再执行一次合并操作,现在两个分支成功合并为同一个分支。
当我们切换会master分支后,将test.txt文件中的内容修改后直接git merge test
会出现如下错误:
zechaowei@zechaoweideMacBook-Pro git % git merge test
error: Merging is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
这个错误提示是Git合并分支时遇到未解决的冲突,需要手动解决后再进行提交。此时需要git commit -a -m 'master merge'
即可,然后进行分支合并git merge test
即可合并成功;
zechaowei@zechaoweideMacBook-Pro git % git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: text/test.txt
no changes added to commit (use "git add" and/or "git commit -a")
zechaowei@zechaoweideMacBook-Pro git % clear
zechaowei@zechaoweideMacBook-Pro git % git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: text/test.txt
no changes added to commit (use "git add" and/or "git commit -a")
zechaowei@zechaoweideMacBook-Pro git % git commit -a -m 'master merge'
[master 45b4840] master merge
zechaowei@zechaoweideMacBook-Pro git % git merge test
Already up to date.
zechaowei@zechaoweideMacBook-Pro git % git log --all --graph --oneline
* 45b4840 (HEAD -> master) master merge
|\
| * e3bb93e (test) branch test commit
* | 62b4452 branch master commit
|/
* 843d723 branch master commit
* e885a98 Modify on master
* fe32bc0 hello world
最后输入git log --all --graph --oneline
可以直观的看见两个分支已经合并了,至此,合并操作也就告一段了
变基分支
除了直接合并分支以外,我们还可以进行变基操作,它跟合并不同,合并是分支回到主干的过程,而变基是直接修改分支开始的位置,比如我们希望将test变基到master上,那么test会将分支起点移动到master最后一次提交位置:
git rebase master
变基后,test分支相当于同步了此前master分支的全部提交。
这里是因为修改了同一个文件产生了冲突,大家可以自己测试一下,没有冲突显示如下命令:
zechaowei@zechaoweideMacBook-Pro git % git rebase master
Successfully rebased and updated refs/heads/test.
变基后,test分支相当于同步了此前master分支的全部提交。(最明显的显示就是执行git log
命令时,没有产生分支)