git 基本指令
Posted GoldenaArcher
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了git 基本指令相关的知识,希望对你有一定的参考价值。
git 基本指令
Bash 指令
在开始之前先介绍一些基础的 bash 指令,如果没有特指 windows 的话,就双(三)端通用 (大概?现在不怎么用 win 了不太确定 win 上能不能跑)。
导航相关
# list 的缩写(大概
➜ ~ ls
Applications Music error.png
# 打开当前所在的文档
➜ ~ open . # mac
> start . # windows
# 打印现在所在文档的路径
➜ ~ pwd
/Users/louhan
# change directory 的缩写,变更所在文档
➜ ~ cd Music
➜ Music ls
Music
➜ Music pwd
/Users/louhan/Music
文件/文件夹相关
# 新建文档
➜ Music touch test.txt
➜ Music ls
Music test.txt
➜ Music touch test2.js test3.python
➜ Music ls
Music test.txt test2.js test3.python
# 删除文档
➜ Music rm test.txt test2.js test3.python
➜ Music ls
Music
# 创建文件夹
➜ Music ls
Music
➜ Music mkdir example
➜ Music ls
Music example
➜ Music cd example
➜ example pwd
/Users/louhan/Music/example
➜ example cd ..
# 删除文件夹
➜ Music rmdir example
➜ Music ls
Music
# mac
# Windows 上使用 -rf 好像会报错
➜ Music mkdir deleteMe
➜ Music ls
Music deleteMe
➜ Music rm deleteMe
rm: deleteMe: is a directory
➜ Music rm -rf deleteMe
➜ Music ls
Music
Git 相关
安装
Windows 下载安装器然后一路 OK 过去就好了。
# mac
➜ ~ brew install git
➜ ~ git --version
git version 2.37.0 (Apple Git-136)
配置用户名和邮箱
主要修改邮箱和显示的名字,换电脑还是要检查一下两个是不是一样,我就碰到过写学校作业的时候不一样,然后评分出了问题还得找教授的麻烦(……)
# set username
➜ ~ git config --global user.name "GoldenaArcher"
# get username
➜ ~ git config user.name
GoldenaArcher
# same thing with email
➜ ~ git config user.email
➜ ~ git config --global user.name "random@email.com"
查看状态以及初始化 git 文件夹
git status
用来查看当前文件夹下的 git 状态
git init
将当前文件夹初始化为一个新的 git 文件夹
下面一段命令可以跳过了
# 在非 git 文件夹下输入该指令会有对应提示
➜ Music git status
fatal: not a git repository (or any of the parent directories): .git
# 换了一个 git 的文件夹
➜ cpAutomation git:(main) ✗ git status
On branch main
Your branch is up to date with 'origin/main'.
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: package.json
Untracked files:
(use "git add <file>..." to include in what will be committed)
cy.md
cypress/integration/examples/Test2.js
cypress/integration/examples/Test3.js
cypress/videos/
error.png
timeout.png
no changes added to commit (use "git add" and/or "git commit -a")
# init 每个 repo 运行一次即可
➜ cypress mkdir testInit
➜ cypress cd testInit
➜ testInit git status
fatal: not a git repository (or any of the parent directories): .git
➜ testInit git init
Initialized empty Git repository in /Users/louhan/study/cypress/testInit/.git/
➜ testInit git:(main) git status
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
.git
文件夹包含了所有 git 相关的内容,如果删掉会导致当前文件夹不再是一个 git 文件夹:
➜ testInit git:(main) ls
➜ testInit git:(main) ls -a
. .. .git
➜ testInit git:(main) git status
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
➜ testInit git:(main) rm -rf .git
➜ testInit git status
fatal: not a git repository (or any of the parent directories): .git
最好不要在 git repo 之中再初始化一个 git repo,有可能会出错
git 基础指令
基本上就是一个简单的流程,可以把每一个 commit 都考虑成一个复活点,这样一旦挂了就不用回到开始重新来过。每次 commit 的流程就是添加一些修改过的文件,提供一段有意义的文字,最后推到远程的 repo 上去。
整体流程大致如下:
其中:
working directory
指的是本地正在工作的目录;
staging area
是 git 在本地保存的一个区域,用来存储目前添加的已修改文档,但是还没有推到当前分支上
git commit
将当前的变化推到当前分支上(还在本机),但是还没有推到对应的远程 repo(即云端上的 repo,比如 GitHub、GitLab 这种)
git add
git add
用来添加修改过的文档。
显示 修改过 但是没有保存到 staging area 的文档
➜ basic git:(main) touch out.txt
➜ basic git:(main) ✗ touch characters.txt
➜ basic git:(main) ✗ git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
characters.txt
out.txt
nothing added to commit but untracked files present (use "git add" to track)
下一步将文件保存到 staging area 中:
➜ basic git:(main) ✗ git add out.txt
➜ basic git:(main) ✗ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: out.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
characters.txt
➜ basic git:(main) ✗ git add characters.txt
➜ basic git:(main) ✗ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: characters.txt
new file: out.txt
git cmomit
commit 就是将推到 staging area 的文档保存到本地的分支中:
# 会打开 vim
➜ basic git:(main) ✗ git commit
# 或者直接保存消息
➜ basic git:(main) ✗ git commit -m "start work on outline and characters"
[main (root-commit) f8b2e15] start work on outline and characters
2 files changed, 3 insertions(+)
create mode 100644 characters.txt
create mode 100644 out.txt
➜ basic git:(main) git status
On branch main
nothing to commit, working tree clean
之后更新一下 out.txt
以及添加一个新的文档。这个时候这两个文档与本地分支上的文档已经不一致了,所以使用 git status
会重新这两个修改过的文档产生了变化。
➜ basic git:(main) git status
On branch main
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: out.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
ch1.txt
no changes added to commit (use "git add" and/or "git commit -a")
➜ basic git:(main) ✗ git add out.txt ch1.txt
➜ basic git:(main) ✗ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: ch1.txt
modified: out.txt
➜ basic git:(main) ✗ git commit -m "update chapter 1"
[main b18e966] update chapter 1
2 files changed, 5 insertions(+)
create mode 100644 ch1.txt
最后可以使用 git log
查看当前分支上所有的记录(已经 commit 的消息):
➜ basic git:(main) git log
commit b18e96680845af42622c630085d003073b21fc79 (HEAD -> main)
Author: Louise Han <louhan@nasdaq.com>
Date: Thu Nov 3 19:46:01 2022 -0400
update chapter 1
commit f8b2e152b28faefa10bc8b0f258581efcb4aa460
Author: Louise Han <louhan@nasdaq.com>
Date: Thu Nov 3 19:41:26 2022 -0400
start work on outline and characters
(END)
以上是关于git 基本指令的主要内容,如果未能解决你的问题,请参考以下文章