赞
踩
git init
.git
的隐藏目录.git
目录是Git用来跟踪管理仓库的,不要手动修改这个目录里面的文件
git config [--global] user.name "Your Name"
git config [--global] user.email "email@example.com"
--global
是一个可选项,表⽰这台机器上所有的Git仓库都会使⽤这个配置git config -l
git config [--global] --unset user.name
git config [--global] --unset user.email
git add [file1] [file2] ...
git add [dir]
git add .
git commit -m "message"
git commit [file1] [file2] ... -m "message"
git commit
后⾯的-m
选项,要跟上描述本次提交的message
,且不能省略
git log
--pretty=oneline
参数7a535a4f3e6dba8a8dbcea9d7e3ee3c395aeabf3
,是每次提交的commit id
(版本号)
SHA1
计算出来的十六进制数rm file
git add file
git commit
git rm file
rm file
和git add file
合并为了一步git commit
git status
$ git status
On branch master
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: 233.txt
no changes added to commit (use "git add" and/or "git commit -a")
git diff [file]
$ git diff
diff --git a/233.txt b/233.txt
index 99a6aad..80c8ca9 100644
--- a/233.txt
+++ b/233.txt
@@ -1 +1,2 @@
SnowK
+DieSnowK
git diff HEAD -- [file]
git reset
,本质是将版本库中的内容进⾏回退,⼯作区或暂存区是否回退由命令参数决定git reset [--soft | --mixed | --hard] [HEAD]
--mixed
:将暂存区和版本库的内容回退到指定版本,工作区的内容不变
--soft
:将版本库的内容回退到指定版本,工作区和暂存区的内容不变--hard
:将工作区、暂存区、版本库的内容都回退到指定版本
[HEAD]
说明:可直接写成commit id/SHA1
,表⽰指定退回的版本
HEAD
表⽰当前版本HEAD^
上⼀个版本HEAD^^
上上⼀个版本~数字
表示
git reset
命令,直接使用之前拿到的git log
中的commit id
即可git log
中的commit id
也找不到了,该怎么办?
git reflog
补救,该命令用来记录本地的每一次命令git reflog
拿到的是一个很简短的哈希值,但是凭借哈希值,也可以回退到特定版本
commit id
的部分commit id
来表示目标版本$ git reflog 1320449 (HEAD -> master) HEAD@{0}: reset: moving to 13204498006394d6067fbc8b6046885c6e5e9649 7a535a4 HEAD@{1}: reset: moving to HEAD^^ 1320449 (HEAD -> master) HEAD@{2}: commit: 233.txt add DieSnowK d74fd33 HEAD@{3}: commit: 666.txt 7a535a4 HEAD@{4}: commit (initial): First Commit $ git reset --hard 7a535a4 HEAD is now at 7a535a4 First Commit $ git log commit 7a535a4f3e6dba8a8dbcea9d7e3ee3c395aeabf3 (HEAD -> master) Author: DieSnowK <1752351098@qq.com> Date: Tue Jul 23 14:30:39 2024 +0800 First Commit
commit id
早就找不到了,此时再想回退倒该版本,貌似就不可能了:PGit在内部有个指向当前分⽀(此处是master
)的HEAD
指针
refs/heads/master
⽂件⾥保存当前master
分⽀的最新commit id
在回退版本的时候,Git仅仅是给refs/heads/master
中存储了一个特定的commit id
add
:只有工作区有当前代码add
,但还未commit
:只有工作区和暂存区有当前代码add
,并且commit
:工作区、暂存区、版本库都有当前代码git checkout -- [filename]
--
不能丢,丢了就是其他含义了git reset --mixed [HEAD]
,将暂存区的内容回退到指定的版本
add
的情况
git checkout -- [filename]
即可commit
后没有push
到远端仓库git reset --hard HEAD^
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。