您的位置:首页 > 其它

Git基本命令操作实例

2014-03-28 17:44 330 查看
Git官网上有code school, 跟着做,以下是具体的记录:

1. 初始化

git init

//Initialized empty Git repository in /.git/

2. 查看repo当前状态

   查看是否有文件增加,修改等(先新建一个文件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)

3. 将新的文件放入暂存区stage,这样的话文件octocat就被追踪了(管理)
git add octocat.txt

//Nice job, you've added octocat.txt to the Staging Area

4. git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
# new file:   octocat.txt

5. 提交更改
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

6. 新增一类文件到暂存区stage

git add '*.txt'

7. git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# new file:   blue_octocat.txt
# new file:   octofamily/baby_octocat.txt
# new file:   octofamily/momma_octocat.txt
# new file:   red_octocat.txt

8.  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

9. 查看日志
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

10. 为本地库添加远程仓库

git remote add origin https://github.com/try-git/try_git.git
11. 将本地repo推送到远程库
git push -u origin master
//Branch master set up to track remote branch master from origin.
//he name of our remote is origin and the default local branch name is master. 

//The -u tells Git to remember the parameters, so that next time we can simply run git push and Git will know what to do.

12. 从远程库中下拉更新
git pull origin master
//We can check for changes on our GitHub repository and pull down any new changes
 Updating 3852b4d..3e70b0f

   Fast-forward

   yellow_octocat.txt |    1 +

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

13. 差异比较
git diff HEAD
//the diff of our most recent commit, which we can refer to using the HEAD pointer
diff --git a/octocat.txt b/octocat.txt
index 7d8d808..e725ef6 100644
--- a/octocat.txt
+++ b/octocat.txt
@@ -1 +1 @@
-A Tale of Two Octocats
+[mA Tale of Two Octocats and an Octodog

14. git add octofamily/octodog.txt

15. 查看暂存区的diff

git diff --staged
diff --git a/octofamily/octodog.txt b/octofamily/octodog.txt
new file mode 100644
index 0000000..cfbc74a
--- /dev/null
+++ b/octofamily/octodog.txt
@@ -0,0 +1 @@

+[mwoof

16. 从暂存区中移除octofamily/octodog.txt
git reset octofamily/octodog.txt

//git reset did a great job of unstaging octodog.txt, but you'll notice that he's still there. He's just not staged anymore.

17. undo

git checkout -- octocat.txt

18. 创建分支

git branch clean_up

19. 切换分支

git checkout clean_up

20. 移除文件
git rm '*.txt'

//git rm command which will not only remove the actual files from disk, but will also stage the removal of the files for us.

21. git commit -m "Remove all the cats"

22. git checkout master

23. 合并分支

git merge clean_up

24. 删除分支

git branch -d clean_up

25. 推送到远程库
git push
To https://github.com/try-git/try_git.git
    3e70b0f..fdfa227  master -> master
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  git操作实例