remote 远程仓库

  • master 分支
  • feature 分支
  • git flow

git流程图.png

本地分支

  • clone 项目:git clone git@xxxx.com
    • clone 结束后,本地当前分支是 master 分支,与远端同步
# gsy @ gsydeMacBook-Pro in ~/test [16:47:26]
$ git clone git@github.com:gsy13213009/shell.git # clone 项目
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.

# gsy @ gsydeMacBook-Pro in ~/test [16:47:36]
$ cd shell  # 进入 shell 文件夹
README-ZH.md         README.md            gmr                  image                oh_my_zsh_install.sh

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: master o [16:48:36]
$ git status # 使用 git status 查看当前状态
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean # 没有 commit,本地仓库是干净的
  • 查看远程仓库信息 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
$ 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
# gsy @ gsydeMacBook-Pro in ~/test/shell on git: master o [16:53:20] C:1
$ git checkout -b siyi/fix_problem1
Switched to a new branch 'siyi/fix_problem1'

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 o [16:53:48]
$ git status
On branch siyi/fix_problem1 # 注意看这里,branch 已经不再是 master 了
nothing to commit, working tree clean # 当前工作空间仍然是干净的,没有任何改动

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 o [16:56:14]
$ 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
  • 修改文件后,查看文件状态
# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [17:03:00]
$ vi README.md # 这个操作修改了 README.md 文件的部分内容
# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [17:05:56]
$ rm oh_my_zsh_install.sh # 删除了 oh_my_zsh_install.sh 文件
# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [17:06:02]
$ touch hhhh.txt # 新建了一个 hhhh.txt 文件

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [17:06:02]
$ git status # 使用 git status 查看当前工作空间状态
On branch siyi/fix_problem1
Changes not staged for commit: # 修改没有被 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 # modified 表示该文件有修改
    deleted:    oh_my_zsh_install.sh # deleted 表示该文件被删除

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    hhhh.txt # Untracked 表示该文件不在 git 的索引(记录)里面,需要使用 `git add <file>` 去将文件添加到 git 索引中

no changes added to commit (use "git add" and/or "git commit -a")
  • 查看文件改动 git diff <file> 尖括号的意思,是可选,也即是可以指定某一个文件,也可以不填,直接使用git diff,会 diff 当前目录下的所有被改动的文件

image

  • 输入 q 退出浏览模式

index stage 暂存区

  • git add README.md 添加README.md
# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [17:17:05]
$ git add README.md # 只添加README.md

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [17:19:03]
$ git status # 查看状态
On branch siyi/fix_problem1
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   README.md # README.md文件出现在 stage 区域里

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 # 处于没有被 track 的状态
  • git add . 添加当前目录下所有被改动的文件到暂存区
# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [17:19:07]
$ git add . # 添加所有文件到 stage 区

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [17:21:17]
$ git status # 所有文件均到了 stage 区,等待 commit
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


# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [21:58:58]
$ git reset README.md
Unstaged changes after reset:
M    README.md

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [21:59:02]
$ git status # 查看状态,可以看 README.md 已经不再 stage 区里面了
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 # reset 后,被移动回工作区了
将 stage 内的改动提交
  • 使用git status查看缓存区的文件,使用git diff --cached查看缓存区的改动详情
  • 确认修改无误后,使用git commit -m "这是第一个提交"提交 stage 区的改动
# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [22:11:40]
$ 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


# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [22:11:42]
$ 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

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [22:11:54]
$ git status # 现在只有README.md处于工作区了,刚刚那两个处于 stage 区的已经被 commit 了
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 # 使用 git log 可以查看 commit 内容
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推送代码到远端
# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [22:18:04]
$ git push # 使用 git push,会提示远端没有这个分支(current branch siyi/fix_problem1 has no upstream branch)
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


# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [22:19:02] C:128
$ 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'. # 推送成功了
  • 此时就可以去 gitlab/github 平台,根据刚刚推的分支创建 merge request 了(其实点击那个 https://github.com/xxxxx 就可以直接跳转到创建合并请求的页面)

image

  • 此时的 git status,还有一个文件正在改动(因为刚刚commit 时,这个 README.md 文件不在 stage 区)
# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [22:19:36]
$ 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文件的修改(目前也只有这个文件)
# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [22:26:09]
$ git checkout .
Updated 1 path from the index

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 o [22:29:26]
$ 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将提交推送到远端分支
# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 o [22:29:29]
$ touch hhhhhhhhh.txt

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [22:30:30]
$ 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)

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [22:30:31]
$ git add .

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [22:30:43]
$ 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


# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 x [22:30:46]
$ git commit -m "第二次测试提交,之前这个分支已经推送到远端了"
[siyi/fix_problem1 235da93] 第二次测试提交,之前这个分支已经推送到远端了
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hhhhhhhhh.txt

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 o [22:31:01]
$ 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

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 o [22:31:07]
$ 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

# gsy @ gsydeMacBook-Pro in ~/test/shell on git: siyi/fix_problem1 o [22:31:25]
$ git status
On branch siyi/fix_problem1
Your branch is up to date with 'origin/siyi/fix_problem1'.

nothing to commit, working tree clean

results matching ""

    No results matching ""