git 基本操作介绍

remote 远程仓库

  • master 分支
  • feature 分支

本地分支

  • 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> <file> 尖括号的意思,是可选,也即是可以指定某一个文件,也可以不填,直接使用git diff,会 diff 当前目录下的所有被改动的文件
  • image.png
  • 输入 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.png
  • 此时的 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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,233评论 6 495
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,357评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,831评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,313评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,417评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,470评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,482评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,265评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,708评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,997评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,176评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,827评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,503评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,150评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,391评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,034评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,063评论 2 352

推荐阅读更多精彩内容

  • git 是什么? git 是目前世界上最先进的分布式版本控制系统。 git与SVN的主要区别 Git是分布式版本控...
    紧张的牛排阅读 4,346评论 0 17
  • 1.在GitHub上创建了一个新项目,如何将本地的一个工程上传上去 2.创建分支 3.从git地址clone 4....
    任振铭阅读 670评论 0 0
  • 一、Git的概念: Git是Linus花了两周的时间用C语言编写的一个版本控制系统,它是目前世界上最先进的分布式版...
    天津的树懒阅读 373评论 0 0
  • 渐变的面目拼图要我怎么拼? 我是疲乏了还是投降了? 不是不允许自己坠落, 我没有滴水不进的保护膜。 就是害怕变得面...
    闷热当乘凉阅读 4,241评论 0 13
  • 夜莺2517阅读 127,717评论 1 9