Git基本使用一【添加文件到版本库中】

书接上回:

  https://www.cnblogs.com/chenliangc/articles/11496123.html

 创建版本库

[root@temp ~]# mkdir /git #创建一个/git目录,这个目录下可能存放多个项目哈
[root@temp ~]# cd /git/   #进入到/git目录下
[root@temp git]# git init project1#创建project1(项目1)目录
Initialized empty Git repository in /git/project1/.git/
[root@temp git]# ll -a project1/ #可以看到project1目录下有隐藏的文件
total 0
drwxr-xr-x 3 root root  18 Sep  9 22:51 .
drwxr-xr-x 3 root root  22 Sep  9 22:51 ..
drwxr-xr-x 7 root root 119 Sep  9 22:51 .git

创建新文件并把文件添加到版本库中

[root@temp ~]# cd /git/project1/   #进入到/git/project1这个目录(项目/工作区)        
[root@temp project1]# echo "1" >>test1.txt #创建test1.txt文件,并追加内容
[root@temp project1]# cat test1.txt #查看test1.txt文件
1
[root@temp project1]# git status #看一看git的状态
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test1.txt
nothing added to commit but untracked files present (use "git add" to track) 
[root@temp project1]# git add test1.txt #把test1.txt文件添加到暂存区
[root@temp project1]# git status   #查看git的状态
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   test1.txt
#
[root@temp project1]# git commit -m "conten 1" #将暂存区的文件提示到版本库中
[master (root-commit) 80f761e] conten 1
 1 file changed, 1 insertion(+)
 create mode 100644 test1.txt
[root@temp project1]# git status  #再查看git的状态,暂存区里已经没有任何文件了
# On branch master
nothing to commit, working directory clean

 修改文件并把文件添加到版本库中

[root@temp project1]# pwd #当前路径
/git/project1
[root@temp project1]# ll *  #查看当前目录下有哪些文件
-rw-r--r-- 1 root root 2 Sep  9 23:29 test1.txt
[root@temp project1]# cat test1.txt  #查看test1.txt文件内容
1
[root@temp project1]# echo "2" >>test1.txt #添加内容到test1.txt文件中
[root@temp project1]# cat test1.txt   #查看test1.txt文件的内容
1
2
[root@temp project1]# git status #查看当前git的状态
# 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:   test1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@temp project1]# git add test1.txt #把test1.txt文件添加到暂存区
[root@temp project1]# git status  #查看git的状态      
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   test1.txt
#
[root@temp project1]# git commit -m "conten 2"  #提交暂存区的文件到版本库中
[master 7e9eae9] conten 2
 1 file changed, 1 insertion(+)
[root@temp project1]# git status #查看当前git的状态  
# On branch master
nothing to commit, working directory clean

总结

对于新增(新创建的文件)你需要用git add命令将其添加到暂存区里面,然后再用git commit命令提交到版本库中

对于现有的文件,你只要一修改,它就在工作区了,你可以用git add命令再添加一次到暂存区,然后用git commit
提交到版本库。也可以直接用git commit -a -m
直接提交到版本库

对于新创建的文件(之前不存在的),你得按照以下流程来进行提交:
工作区(创建新文件)->git add fileN->暂存区->git commit -m "xx"->分支->版本库 对现有文件进行修改后,可以有以下两种提交的流程: -- 第一种流程 工作区(修改文件)->git add fileN->暂存区->git commit -m "xx"->分支->版本库 -- 第二种流程 工作区(修改文件)->git commit -a -m "xx"->分支->版本库 

转载于:https://www.cnblogs.com/chenliangc/articles/11496579.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值