【git学习一】git add,commit的几个命令

2017-12-25  本文已影响0人  cancan18

命令

工作区、暂存区和分支

这几个简单命令的操作会将数据从工作区添加到版本库里面。

image.png

命令的练习和总结

在此版本下进行操作:git version 2.13.0
总结:

  1. git add change-me delete-me 【提交到暂存区,指定文件,将工作区的文件提交到暂存区】

  2. git add . 与 git add -A 【会将所有的工作区的文件放入暂存区】

  3. git add -u 【提交被修改(modified)和被删除(deleted)文件,不包括新文件(new)】

  4. git rm --cached change-me delete-me 将暂存区移动到工作区
    当我们需要删除暂存区或分支上的文件, 同时工作区也不需要这个文件了, 可以使用
    git rm file_path
    当我们需要删除暂存区或分支上的文件, 但本地又需要使用, 只是不希望这个文件被版本控制, 可以使用
    git rm --cached file_path
    对于之前已经在版本库里面,使用git rm --cached 会将文件移除版本库,并放在工作区。这个时候文件对于版本来说是新增的。
    对于之前不再版本库中,就变成将暂存区移动到工作区。

  5. git reset 【将暂存区的东西重置】
    git rm -- cached 是从stage(index,暂存区) 里面删除文件,当你提交(commit)之后文件就会删除了,就是移除版本库。。
    git reset HEAD -- file : 回退暂存区里的文件(还原为HEAD commit里面该文件的状态),会撤销从上一次提交(commit)之后的一些操作。
    如果是对于新增文件,这两个操作时等效的。
    这两个命令都是对stage,index的操作。
    git reset HEAD file (命令默认参数为 --mixed)
    git reset命令有三种模式:git reset [--<mode>] [<commit>]

    该命令用来回退当前的分支head 到 <commit>, 根据<mode>不同 --soft --mixed --hard,会对working tree和index和HEAD进行重置, mode分为以下几种

    1) --soft: 回退到某个版本,只回退了commit的信息,不会恢复到index file一级(对索引不做改动)。如果还要提交,直接commit即可,执行该模式之后,git status 对所有修改的文件会提示(Changes to be committed)
    2) --mixed: 此为默认方式,不带任何参数的git reset,即是这种方式,它回退到某个版本,只保留源码,回退commit和index信息。
    3) --hard: 彻底回退到某个版本,本地的源码也会变为上一个版本的内容(所有的修改都会被丢弃)。

  6. git commit -am 【a必须写在m的前面】
    对于已经在版本库(即git commit 过的文件)或者在暂存区的(即git add 过的文件),修改过的话,就可以直接使用git commit -am 进行提交,省略git add 的步骤
    直接使用会出现下面的错误
    $ git commit -am 'xx' 【对于没有在版本库的或者暂存区直接使用这个命令的话,会提示下面这】
    On branch master
    Untracked files:
    xx

    nothing added to commit but untracked files present

练习

test@m MINGW64 /d/test/test_git
$ git init
Initialized empty Git repository in D:/test/test_git/.git/

$ echo change me > change-me
$ echo delete me > delete-me

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        change-me
        delete-me

nothing added to commit but untracked files present (use "git add" to track)

test@m MINGW64 /d/test/test_git (master)
$ git add change-me delete-me #提交到暂存区,指定文件,将工作区的文件提交到暂存区

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)【当时新增文件的时候,会提示你使用git rm --cached 】

        new file:   change-me
        new file:   delete-me


test@m MINGW64 /d/test/test_git (master)
$ git rm --cached change-me delete-me 将暂存区移动到工作区
rm 'change-me'
rm 'delete-me'

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        change-me
        delete-me

nothing added to commit but untracked files present (use "git add" to track)

test@m MINGW64 /d/test/test_git (master)
$ git commit -m 'inital' 提交
[master (root-commit) 1bdcf48] inital
 2 files changed, 2 insertions(+)
 create mode 100644 change-me
 create mode 100644 delete-me

$ echo ok >> change-me
$ rm delete-me
$ echo Add me > add-me

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   change-me
        deleted:    delete-me

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        add-me

no changes added to commit (use "git add" and/or "git commit -a")

test@m MINGW64 /d/test/test_git (master)
$ git add . 【会将所有的工作区的文件放入暂存区,与命令git add -A 效果一样】

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   add-me
        modified:   change-me
        deleted:    delete-me

test@m MINGW64 /d/test/test_git (master)
$ git reset 【将暂存区的东西重置】
Unstaged changes after reset:
M       change-me
D       delete-me

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   change-me
        deleted:    delete-me

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        add-me

no changes added to commit (use "git add" and/or "git commit -a")

$ git add -u 【提交被修改(modified)和被删除(deleted)文件,不包括新文件(new)】

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   change-me
        deleted:    delete-me

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        add-me


test@m MINGW64 /d/test/test_git (master)
$ echo >> add-me

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   add-me

no changes added to commit (use "git add" and/or "git commit -a")

test@m MINGW64 /d/test/test_git (master)
$ git add .

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)【当是更改文件的时候,会提示你使用git reset HEAD file 撤销暂存区的东西】

        modified:   add-me


test@m MINGW64 /d/test/test_git (master)
$ git rm --cached add-me
rm 'add-me'

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    add-me

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        add-me
$ git commit -m 'xx' 【这个时候,就会从index中删除该文件,也就是不在版本库里面了】

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        add-me

nothing added to commit but untracked files present (use "git add" to track)

test@m MINGW64 /d/test/test_git (master)
$ git add .

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   add-me 【这个时候文件就变成新增的,因为我们使用git rm --cache进行删除】


test@m MINGW64 /d/test/test_git (master)
$ ls
add2.txt  add3.txt  add-me  change-me  ddxx

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ddxx

nothing added to commit but untracked files present (use "git add" to track)

test@m MINGW64 /d/test/test_git (master)
$ git add .

test@m MINGW64 /d/test/test_git (master)
$ ls
add2.txt  add3.txt  add-me  change-me  ddxx

test@m MINGW64 /d/test/test_git (master)
$ ls
add2.txt  add3.txt  add-me  change-me  ddxx

test@m MINGW64 /d/test/test_git (master)
$ echo xx > ddxx

test@m MINGW64 /d/test/test_git (master)
$ ls
add2.txt  add3.txt  add-me  change-me  ddxx

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   ddxx

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   ddxx


test@m MINGW64 /d/test/test_git (master)
$ git commit -am 'x' 【对于已经在版本库或者再暂存区的文件,可以直接使用 -am直接提交。可以省略git add 】
[master 943ea92] x
 1 file changed, 1 insertion(+)
 create mode 100644 ddxx

test@m MINGW64 /d/test/test_git (master)
$ echo mm > xx

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        xx

nothing added to commit but untracked files present (use "git add" to track)

test@m MINGW64 /d/test/test_git (master)
$ git commit -am 'xx' 【对于没有在版本库的或者暂存区直接使用这个命令的话,会提示下面这】
On branch master
Untracked files:
        xx

nothing added to commit but untracked files present

test@m MINGW64 /d/test/test_git (master)
$ git reset

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        xx

nothing added to commit but untracked files present (use "git add" to track)

test@m MINGW64 /d/test/test_git (master)
$ ls
add2.txt  add3.txt  add-me  change-me  ddxx  xx

test@m MINGW64 /d/test/test_git (master)
$ > ddxx

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   ddxx

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        xx

no changes added to commit (use "git add" and/or "git commit -a")

test@m MINGW64 /d/test/test_git (master)
$ git commit -am 'x'
[master 96bb4c2] x
 1 file changed, 1 deletion(-)

test@m MINGW64 /d/test/test_git (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        xx

nothing added to commit but untracked files present (use "git add" to track)
上一篇下一篇

猜你喜欢

热点阅读