書接上回:
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"->分支->版本庫