基本入库
修改一个文件
假设修改了a.txt。
使用git status可以看到Changes not staged for commit下面有一个modified的文件
[smbtest@localhost a]$ 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: a.txt
#
no changes added to commit (use "git add" and/or "git commit -a")git add
使用git add后,再用git status查看有一个Changes to be commited的文件
[smbtest@localhost a]$ git add a.txt
[smbtest@localhost a]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a.txt
#
使用git commit后,git status内没有内容,git log可以看到新增一条log
[smbtest@localhost a]$ git commit -m "modify a.txt"
[master 22316f1] modify a.txt
1 file changed, 1 insertion(+)
[smbtest@localhost a]$ git status
# On branch master
nothing to commit(working directory clean)
[smbtest@localhost a]$ git log
commit 22316f1e002341ec33f58d7c82730527625237c0
Author:
Date: Sun Feb 2 10:49:03 2014 +0800
modify a.txt
使用git remote add,添加一个remote(本命令只需使用一次)
[smbtest@localhost a]$ git remote add origin2 ssh://xxx
使用git push,将本地修改推送到远端库
[smbtest@localhost a]$ git push
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 338 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://git@sny-jira.ads.finisar.com:7999/gpon/test.git
ed7f63f..22316f1 master -> master
增加一个文件
使用git status可以看到一个untracked files
[smbtest@localhost a]$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# a2.txt
nothing added to commit but untracked files present (use "git add" to track)
使用git add后,再用git status查看有一个Changes to be commited的文件
[smbtest@localhost a]$ git add a2.txt
[smbtest@localhost a]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: a2.txt
#
使用git commit后,git status内没有内容,git log增加了一条log
[smbtest@localhost a]$ git commit -m "add a2.txt"
[master 21a90f2] add a2.txt
1 file changed, 1 insertion(+)
create mode 100644 a2.txt
[smbtest@localhost a]$ git status
# On branch master
nothing to commit (working directory clean)
[smbtest@localhost a]$ git log
commit 21a90f2562c826230adfbcc5a1332a7f081344a9
Author:
Date: Sun Feb 2 11:01:06 2014 +0800
add a2.txt
直接使用git push将本地修改推送到远端库
[smbtest@localhost a]$ git push
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 284 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To ssh://git@sny-jira.ads.finisar.com:7999/gpon/test.git
22316f1..21a90f2 master -> master
删除一个文件
[smbtest@localhost a]$ git rm a2.txt
rm 'a2.txt'
[smbtest@localhost a]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: a2.txt
#
[smbtest@localhost a]$ git commit -m "delete a2.txt"
[master 3764a36] delete a2.txt
1 file changed, 1 deletion(-)
delete mode 100644 a2.txt
[smbtest@localhost a]$ git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 227 bytes, done.
Total 2 (delta 1), reused 0 (delta 0)
To ssh://git@sny-jira.ads.finisar.com:7999/gpon/test.git
21a90f2..3764a36 master -> master
小结
从上述操作可以看到,git的操作分为三个层次,stage、local repo和remote repo,其中git add和git rm用于将一个文件的修改(包括创建和删除)添加到stage,git commit用于将stage中记录的修改入库到local repo,git push则将本地库的修改推送到远端库,每个层次的操作都以前一层次的内容为基础——即stage内有内容才可以commit,local repo有内容才可以push。