> git init
Initialized empty Git repository in /.git/
Success!
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
Success!
当创建一个文件:octocat.txt 后
$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# octocat.txt
nothing added to commit but untracked files present (use "git add" to track)
现在要把这个文件加入到git的准备区域(staging area):
$ git add octocat.txt
$ git status
$ git commit -m "Add cute octocat story"
[master (root-commit) 20b5ccd] Add cute octocat story
1 file changed, 1 insertion(+)
create mode 100644 octocat.txt
现在目录底下又添加了多个txt文件:
$ git add '*.txt'
$ git commit -m 'Add all the octocat txt files'
[master 3852b4d] Add all the octocat txt files
4 files changed, 4 insertions(+)
create mode 100644 blue_octocat.txt
create mode 100644 octofamily/baby_octocat.txt
create mode 100644 octofamily/momma_octocat.txt
create mode 100644 red_octocat.txt
可以查看commit记录:
$ git log
commit 3852b4db1634463d0bb4d267edb7b3f9cd02ace1
Author: Try Git <try_git@github.com>
Date: Sat Oct 10 08:30:00 2020 -0500
Add all the octocat txt files
commit b652edfd888cd3d5e7fcb857d0dabc5a0fcb5e28
Author: Try Git <try_git@github.com>
Date: Sat Oct 10 08:30:00 2020 -0500
Added cute octocat story
现在在Github有一个repository:https://github.com/try-git/try_git.git
现在要配置,把这个地址作为远程的repository
$ git remote add origin https://github.com/try-git/try_git.git
现在远程的repository名叫做origin,默认本地的分支叫做master,-u是让git记住参数,下一次就可以直接git push
$ git push -u origin master
Branch master set up to track remote branch master from origin.
从github上下载到本地:
$ git pull origin master
Updating 3852b4d..3e70b0f
Fast-forward
yellow_octocat.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 yellow_octocat.txt
现在有一些变化,可以用git diff查看变化,HEAD表示最近一次commit
$ git diff HEAD
还可以用diff来查看已经被staged(即未被commit)的文件
所以先添加一个文件:
$ git add octofamily/octodog.txt
$ git diff --staged
可以用git reset来unstage一个staged的文件:
$ git reset octofamily/octodog.txt
可以用git checkout -- <target> 回到最后一次commit <target>
$ git checkout -- octocat.txt
创立一个新的branch:
$ git branch clean_up
切换到<branch>: git checkout <branch>
$ git checkout clean_up
Switched to branch 'clean_up'
用git rm移除所有硬盘中的文件和staged的文件
$ git rm '*.txt'
rm 'blue_octocat.txt'
rm 'octocat.txt'
rm 'octofamily/baby_octocat.txt'
rm 'octofamily/momma_octocat.txt'
rm 'red_octocat.txt'
$ git commit -m "Remove all the cats"
[clean_up 63540fe] Remove all the cats
5 files changed, 5 deletions(-)
delete mode 100644 blue_octocat.txt
delete mode 100644 octocat.txt
delete mode 100644 octofamily/baby_octocat.txt
delete mode 100644 octofamily/momma_octocat.txt
delete mode 100644 red_octocat.txt
$ git checkout master
合并clean_up branch:
$ git merge clean_up
Updating 3852b4d..ec6888b
Fast-forward
blue_octocat.txt | 1 -
octocat.txt | 1 -
octofamily/baby_octocat.txt | 1 -
octofamily/momma_octocat.txt | 1 -
red_octocat.txt | 1 -
5 files changed, 5 deletions(-)
delete mode 100644 blue_octocat.txt
delete mode 100644 octocat.txt
delete mode 100644 octofamily/baby_octocat.txt
delete mode 100644 octofamily/momma_octocat.txt
delete mode 100644 red_octocat.txt
可以用git branch -d <branch name> 来删除某个branch
$ git branch -d clean_up
Deleted branch clean_up (was ec6888b).
$ git push
To https://github.com/try-git/try_git.git
3e70b0f..132b453 master -> master