Git基本操作(二)

本地库操作

1. 查看(status)

  • git status

查看当前仓库状态

$ git status
On branch master (在master分支上)   
   
Initial commit  
 
nothing to commit (create/copy files and use "git add" to track)   
  • git log

查看当前仓库的历史日志

$ git log  

如果想看到修改了哪些行等更详细的信息可以使用:

$ git log -p

2. 添加内容(add)

将文件加入到缓存区域

  • 创建新文件

    $ echo “Hello Git” > a.txt
    
  • 添加

    $ git add a.txt # 将a.txt添加到暂存区中
    
  • 打点

    $ git add . # 
    

这个命令要求git给我目前的这个项目制作一个快照snapshot(快照只是登记留名,快照不等于记录在案,git管快照叫做索引index)。快照一般会暂时存储在一个临时存储区域中。

  • 查看

    $ git status #查看当前仓库的状态
    On branch master
    
    Initial commit  
    
    Changes to be committed:(暂存里下次将被提交的修改)  
    (use "git rm --cached <file>..." to unstage)  
    
    new file: a.txt  
    

3. 提交(commit)

将缓存区域的文件提交到本地版本库,加上-a选项则将已跟踪但未缓存的文件也包括在内。

$ git commit -m "project init" #将刚才的修改提交到本地仓库中 
[master (root-commit) 62dd222] Project init
Committer: breakerthb <breakerthb@126.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

1 file changed, 1 insertion(+)
create mode 100644 a.txt

如果没有-m写日志,会弹出vim窗口要求填写commit日志

$ git status  
On branch master  
nothing to commit, working directory clean    

现在你执行一下git log 命令就会看到刚才的提交记录

$ git log
commit 62dd2225d195630ed9b5e493333b84b40bae30c9
Author: breakerthb <breakerthb@126.com>
Date:   Sat Oct 24 11:00:57 2015 +0800

Project init

“62dd2225d195630ed9b5e493333b84b40bae30c9”这一串字符就是我们这次创建的提交的名字。看起来是不是很熟,如果经常用电驴的朋友就会发现它就是和电驴里内容标识符一样,都是SHA1串。Git通过对提交内容进行 SHA1 Hash运算,得到它们的SHA1串值,作为每个提交的唯一标识。根据一般的密码学原理来说,如果两个提交的内容不相同,那么它们的名字就不会相同;反之,如果它们的名字相同,就意味着它们的内容也相同。

4. 删除(rm)

删除文件

$ git rm

--cached选项只删除索引,保留文件,即恢复成untracked状态


上一篇:Git基本操作(一)
下一篇:Git基本操作(三)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容