remote 远程仓库
- master 分支
- feature 分支
- git flow

本地分支
- clone 项目:
git clone git@xxxx.com
- clone 结束后,本地当前分支是 master 分支,与远端
同步
$ git clone git@github.com:gsy13213009/shell.git
Cloning into 'shell'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 10
Receiving objects: 100% (13/13), 221.87 KiB | 320.00 KiB/s, done.
Resolving deltas: 100% (2/2), done.
$ cd shell
README-ZH.md README.md gmr image oh_my_zsh_install.sh
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
- 查看远程仓库信息
git remote -v
,origin 是远程仓库的名字,后面是仓库对应的地址
$ git remote -v
origin git@github.com:gsy13213009/shell.git (fetch)
origin git@github.com:gsy13213009/shell.git (push)
$ git status
On branch master // 本地仓库当前分支是 master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean // 没有commit,本地空间是干净的
- 切换/新建分支
git checkout -b siyi/fix_problem1
$ git checkout -b siyi/fix_problem1
Switched to a new branch 'siyi/fix_problem1'
$ git status
On branch siyi/fix_problem1
nothing to commit, working tree clean
$ ls -al
total 72
-rw-r--r-- 1 gsy staff 4.9K Dec 11 16:47 README-ZH.md
-rw-r--r-- 1 gsy staff 5.3K Dec 11 16:47 README.md
-rwxr-xr-x 1 gsy staff 11K Dec 11 16:47 gmr
drwxr-xr-x 3 gsy staff 96B Dec 11 16:47 image
-rw-r--r-- 1 gsy staff 7.9K Dec 11 16:47 oh_my_zsh_install.sh
$ vi README.md
$ rm oh_my_zsh_install.sh
$ touch hhhh.txt
$ git status
On branch siyi/fix_problem1
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
deleted: oh_my_zsh_install.sh
Untracked files:
(use "git add <file>..." to include in what will be committed)
hhhh.txt
no changes added to commit (use "git add" and/or "git commit -a")
- 查看文件改动
git diff <file>
尖括号的意思,是可选,也即是可以指定某一个文件,也可以不填,直接使用git diff
,会 diff 当前目录下的所有被改动的文件

index stage 暂存区
git add README.md
添加README.md
$ git add README.md
$ git status
On branch siyi/fix_problem1
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: oh_my_zsh_install.sh
Untracked files:
(use "git add <file>..." to include in what will be committed)
hhhh.txt
git add .
添加当前目录下所有被改动的文件到暂存区
$ git add .
$ git status
On branch siyi/fix_problem1
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
new file: hhhh.txt
deleted: oh_my_zsh_install.sh
- 此时 commit 便可以将 stage 区的内容生成一个 commit 记录
从 stage 区移除
- 使用
git reset README.md
将README.md移除 stage 区
git reset README.md
只是将README.md移除 stage 区,修改的内容不变,git reset --hard README.md
不仅从 stage 区域移除,连修改的内容也会回滚,也就本地的改动丢弃了
$ git status
On branch siyi/fix_problem1
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
new file: hhhh.txt
deleted: oh_my_zsh_install.sh
$ git reset README.md
Unstaged changes after reset:
M README.md
$ git status
On branch siyi/fix_problem1
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: hhhh.txt
deleted: oh_my_zsh_install.sh
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: README.md
将 stage 内的改动提交
- 使用
git status
查看缓存区的文件,使用git diff --cached
查看缓存区的改动详情
- 确认修改无误后,使用
git commit -m "这是第一个提交"
提交 stage 区的改动
$ git status
On branch siyi/fix_problem1
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: hhhh.txt
deleted: oh_my_zsh_install.sh
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: README.md
$ git commit -m "第一次测试提交"
[siyi/fix_problem1 907b546] 第一次测试提交
2 files changed, 281 deletions(-)
create mode 100644 hhhh.txt
delete mode 100644 oh_my_zsh_install.sh
$ git status
On branch siyi/fix_problem1
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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
--------------------------------------------------------------
git log
commit 907b54640f66b959a0821891d0c3c86313e4b00d (HEAD -> siyi/fix_problem1)
Author: gsy <gsy@gsy.com>
Date: Fri Dec 11 22:11:54 2020 +0800
第一次测试提交
commit aea768fb3cd87e87242c12b8844cd884e42fcabc (origin/master, origin/HEAD, master)
Author: gsy <gsy@gsy.com>
Date: Sun Jan 19 19:53:05 2020 +0800
Create oh_my_zsh_install.sh
commit 90b6f9fe79ddaacc2500aee74d73ed7e83497802
Author: gsy <gsy@gsy.com>
Date: Fri Sep 27 20:05:37 2019 +0800
(CHORE) update script
(END)
- 查看某个 commit 的内容
git show 907b54640f66b959a0821891d0c3c86313e4b00d
推送 commit 到远程仓库
$ git push
fatal: The current branch siyi/fix_problem1 has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin siyi/fix_problem1
$ git push --set-upstream origin siyi/fix_problem1
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 286 bytes | 286.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'siyi/fix_problem1' on GitHub by visiting:
remote: https://github.com/gsy13213009/shell/pull/new/siyi/fix_problem1
remote:
To github.com:gsy13213009/shell.git
* [new branch] siyi/fix_problem1 -> siyi/fix_problem1
Branch 'siyi/fix_problem1' set up to track remote branch 'siyi/fix_problem1' from 'origin'.

- 此时的 git status,还有一个文件正在改动(因为刚刚commit 时,这个 README.md 文件不在 stage 区)
$ git status
On branch siyi/fix_problem1
Your branch is up to date with 'origin/siyi/fix_problem1'.
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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
- 使用
git checkout .
丢弃工作区的所有文件的修改
- 使用
git checkout README.md
丢弃README.md
文件的修改(目前也只有这个文件)
$ git checkout .
Updated 1 path from the index
$ git status
On branch siyi/fix_problem1
Your branch is up to date with 'origin/siyi/fix_problem1'.
nothing to commit, working tree clean
再次修改文件提交
touch
创建一个文件,git add .
将文件添加到 stage 区,git commit -m "xxx"
将 stage 区域的改动提交,git push
将提交推送到远端分支
$ touch hhhhhhhhh.txt
$ git status
On branch siyi/fix_problem1
Your branch is up to date with 'origin/siyi/fix_problem1'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
hhhhhhhhh.txt
nothing added to commit but untracked files present (use "git add" to track)
$ git add .
$ git status
On branch siyi/fix_problem1
Your branch is up to date with 'origin/siyi/fix_problem1'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: hhhhhhhhh.txt
$ git commit -m "第二次测试提交,之前这个分支已经推送到远端了"
[siyi/fix_problem1 235da93] 第二次测试提交,之前这个分支已经推送到远端了
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 hhhhhhhhh.txt
$ git status
On branch siyi/fix_problem1
Your branch is ahead of 'origin/siyi/fix_problem1' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 305 bytes | 305.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:gsy13213009/shell.git
907b546..235da93 siyi/fix_problem1 -> siyi/fix_problem1
$ git status
On branch siyi/fix_problem1
Your branch is up to date with 'origin/siyi/fix_problem1'.
nothing to commit, working tree clean