git 操作详解【小白带入门篇】

1、第一步,git的基本使用
找到一个空文件夹,右键“git bash here"进入git 命令行
a)初始化:
#git config --global user.name "***"
#git config --global user.email "***"
/*以上***为本人在github/giee上注册的用户名和邮箱,由于在国内网络,以Giee为主*/

b)在Giee上新建一个空白仓库new-empty
c)关联远程仓库
#git remote add origin https://gitee.com/Streaky_Porks/new-empty.git
此时会报错:/*fatal: not a git repository (or any of the parent directories): .git*/没有依赖文件夹
需要先初始化:#git init
再次#git remote add origin https://gitee.com/Streaky_Porks/new-empty.git
路径最右侧出现(master)表明关联成功

d)新建文件、提交文件
首先介绍git status 命令,可以随时查看本地仓库与远程仓库的差异情况
在本地当前文件夹新建一个hello.txt文件
此时使用命令#git status
/*On branch master
*No commits yet
*Untracked files:
*  (use "git add <file>..." to include in what will be committed)
*        hello.txt
*nothing added to commit but untracked files present (use "git add" to track)
*/表示在分支 master没有已commit的文件,有一个未追踪的文件(使用git add<file> 加入index区,将被commit)为追踪文件:hello.txt
最后一句总结:无对象加入到待commit但有未追踪文件存在(使用git add追踪)

然后我们尝试#git add hello.txt
#git status
/*
*On branch master
*No commits yet
*Changes to be committed:
 * (use "git rm --cached <file>..." to unstage)
*        new file:   hello.txt
*/表示当前分支没有也commit文件,(使用 git rm --cached <file>来未声明)   新文件hello.txt
我们按它提示的命令尝试
#git rm --cached hello.txt
/*此时再git status发现hello.txt状态回到了未git add区,表明git rm --cached 是把文件从缓存区移除,是git add的退档
#git add hello.txt
#git commit hello.txt
/*提示输入commit的内容【前面用git config --global core.editor vim设置默认输入为vim】
输入后提示commit完成
#git status
/*On branch master
*nothing to commit, working tree clean
*/commit 后hello.txt文件已经上传远程仓库完成
但此时我们在giee网站并不能看到它
查看相应原理图可知commit 只是将文件上传到了储存室Respository还没到达远程仓库Remote

e)验证Index、Respository的作用
我们删除本地文件
#rm hello.txt
#git status
/*On branch master
*Changes not staged for commit:
*  (use "git add/rm <file>..." to update what will be committed)
*  (use "git restore <file>..." to discard changes in working directory)
*        deleted:    hello.txt
*no changes added to commit (use "git add" and/or "git commit -a")
*/可以看到,git status对比了本地和Respository的差异,同时告诉我们已删除: hello.txt
我们# git restore hello.txt可以把文件从Respository下载回到本地
我们试试在新文件夹是否可以操作
新建文件夹
#git init
#git remote add origin https://gitee.com/Streaky_Porks/new-empty.git    /*建立连接*/
#git status 没有反应

此时在两个文件夹同时连接一个远程仓库的情况下;两边各自git add <file> 或是 git commit <file>对无影响
表明Index 和Respotory都是私人空间,算是本地文件夹的一个备份,没有共享功能。

f)提交和下载
#git push -u origin master
/*此时会报错,提醒首次push之前需要先git pull
#git clone https://gitee.com/Streaky_Porks/new-empty.git
因为本地跟远程文件差异太大需要先将远程仓库文件同步到本地
此时我们#ls 发现本地文件夹是这样的
/*hello.txt  new-empty/    */意即当初始化git init后本地current指向的是当前文件夹,而在git remote关联之后,
远程端current指向的是./$仓库名/所在的文件夹,此时先将Remote git clone到本地,在cd $仓库名/之后两个current才能指向同一位置
此时,才可以正确git push/pull
此时已进入new-empty/文件夹,并复制一份文件到本地
#git status 可以看到提示要git add hello.txt
#git add hello.txt
#git commit hello.txt -m "commit hello.txt"
此时,我们在看下Respositery的hello.txt能不能到达远程Remote
#git push
此时,我们在giee网站上的仓库可以看到hello.txt已经上传成功

2、git 分支操作(实际工作常用)
#git branch   /*查看所有分支
#git branch test     /*新建分支test
此时本地就有了两个分支,maste&test ,远端还是保持不变
#git checkout test     /*路径最右侧蓝色字体切换成test表示已经成功切换到test分支
#git push origin test    /*把本地新的分支同步到Remote
在giee上新建一个文件helloword.c并提交
git switch master  /  git switch test
#git pull origin test         /*下载都本地
#git switch master
#git merge test
#git status
/*On branch master
*Your branch is ahead of 'origin/master' by 1 commit.
*  (use "git push" to publish your local commits)

*nothing to commit, working tree clean
表示:有一份文件待git push
#git push
此时在Giee远程仓库master分支可以看到helloword.c文件成功上传
#git branch -d test
删除test分支
更多git 分支操作待后边继续学习更新。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值