Git使用快速入门

Git是一个好用的开源分布式版本控制工具,下面通过几个命令行了解它的使用:

1)配置下身份,这样可以知道是谁在提交代码
 

git config --global user.name "Tim"
git config --global user.email "zhoudaweijsj@126.com"

2)初始化

创建一个代码目录,期望对这个目录进行版本控制,首先我们需要给这个目录创建一个代码仓库,指令很简单:
 git init
在这个目录下会多一个 .git 目录

Tim@Tim-VirtualBox:~/develop/test$ ls -al
……
-rw-rw-r-- 1 Tim Tim   283  4月 14 14:24 dll_main_test.c
-rwxrwxr-x 1 Tim Tim  6960  4月 14 14:05 dll.so
-rw-rw-r-- 1 Tim Tim   227  4月 20 15:42 dll_test.c
drwxrwxr-x 8 Tim Tim  4096  4月 20 16:14 .git

3)新增文件

git add  xxx 新增目录或者文件, 类似于svn的add
git add .      新增当前目录所有项目

4)提交变更

git commit -m "first commit"   同SVN命令类似,这个命令会提交上一步增加的所有项目
git commit -m "first commit"  xxxx 提交对应文件

5)查看工作目录状态

git status  下面是目录没有修改的情况

On branch master
nothing to commit, working directory clean


有文件修改:

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:   dll_test.c

no changes added to commit (use "git add" and/or "git commit -a")

6)增加要忽率的项目

增目录下增加.gitignore文件,并把要忽率的目录增加到这个文件中

7)查看修改的内容

git status

8)查看文件修改内容

git diff xxxx

diff --git a/dll_test.c b/dll_test.c
index d472886..e7893bb 100644
--- a/dll_test.c
+++ b/dll_test.c
@@ -2,7 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-//this is test
+//this is test xx
 int printf_hello_world()
 {
        int i = 0;


9)撤销变更(svn revert)

git checkout xxxx

10)查看修改记录

$ git log

commit 05b81e2cdd364595469c28003ba1f976eadc463d
Author: Tim <zhoudaweijsj@126.com>
Date:   Sat Apr 20 16:41:34 2019 +0800

    t

commit 80fcd1639938e1c56c5a08c091a579fae4ca6e1f
Author: Tim <zhoudaweijsj@126.com>
Date:   Sat Apr 20 16:12:19 2019 +0800

    test commit

查看某一条:

$ git log 80fcd1639938e1c56c5a08c091a579fae4ca6e1f -1
commit 80fcd1639938e1c56c5a08c091a579fae4ca6e1f
Author: Tim <zhoudaweijsj@126.com>
Date:   Sat Apr 20 16:12:19 2019 +0800

    test commit

某一条修改内容:
 

$ git log 80fcd1639938e1c56c5a08c091a579fae4ca6e1f -1 -p
commit 80fcd1639938e1c56c5a08c091a579fae4ca6e1f
Author: Tim <zhoudaweijsj@126.com>
Date:   Sat Apr 20 16:12:19 2019 +0800

    test commit

diff --git a/dll_test.c b/dll_test.c
index f58795e..d472886 100644
--- a/dll_test.c
+++ b/dll_test.c
@@ -2,7 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-
+//this is test
 int printf_hello_world()
 {
        int i = 0;



11)分支操作

$ git branch -a
* master


只有一个master分支存在

创建一个testbranch1分支

$ git branch testbranch1
$ git branch -a
* master
  testbranch1

切换分支

$ git checkout testbranch1
Switched to branch 'testbranch1'
$ git branch -a
  master
* testbranch1

合并
首先切换到要合并的目的分支

$ git checkout master
使用merge合并
$ git merge testbranch1
Updating 05b81e2..0849c47
Fast-forward
 cmd.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git log

删除分支 -D

$ git branch -a
* master
  testbranch1
$ git branch -D testbranch1
Deleted branch testbranch1 (was 0849c47).
$ git branch -a
* master

12)下载代码

git clone https://github.com/test/test.git

提交代码
git push
git fetch origin master
git diff origin/master
git merge origin/master
git pull origin master 相当于fetch和merge
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值