git的常用命令(安装、建仓库、添加、提交、查询历史版本)

最近工作中用到git,其实之前也用过,但是忘的差不多了。现在再来复习一下。

参考了廖老师的网站

 

【我的环境】

我的使用的是2019最新版13.3寸的MacBook Pro。系统是10.15.1。

chens-MacBook-Pro:~ chenchen$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.15.1
BuildVersion:	19B88
chens-MacBook-Pro:~ chenchen$ 

 

第一步, 【安装git】

先百度git,第一个结果是git官网,点击进入。

进入如下页面,选左下角 Download,进入新页面。

然后选对应的操作系统,我选Mac OS X,

然后,会下载一个dmg文件,下载成功后,双击安装一步步进行下去直到完成即可。

同时你还可以下载git的pdf教程。

 

【检查是否安装成功】

可以在terminal里输入git 然后回车。如果显示内容如下所示,则安装成功。

 

第二步,是在本地新建git仓库】

使用 git init,这时会看到当前目录下出现  .git  。

chens-MacBook-Pro:~ chenchen$ ls
Desktop		Downloads	Movies		Pictures
Documents	Library		Music		Public
chens-MacBook-Pro:~ chenchen$ pwd
/Users/chenchen
chens-MacBook-Pro:~ chenchen$ mkdir learn_git
chens-MacBook-Pro:~ chenchen$ pwd
/Users/chenchen
chens-MacBook-Pro:~ chenchen$ cd learn_git/
chens-MacBook-Pro:learn_git chenchen$ pwd
/Users/chenchen/learn_git
chens-MacBook-Pro:learn_git chenchen$ git init
Initialized empty Git repository in /Users/chenchen/learn_git/.git/
chens-MacBook-Pro:learn_git chenchen$ ls -al
total 0
drwxr-xr-x   3 chenchen  staff   96 Nov 30 18:36 .
drwxr-xr-x+ 21 chenchen  staff  672 Nov 30 18:35 ..
drwxr-xr-x  10 chenchen  staff  320 Nov 30 18:36 .git

 

第三步,现在你就可以在刚才init的目录新建文本文件然后提交了。

每次提交都是 git add  git commit,注意git status的使用。

git add是把工作区的修改,添加到暂存区。

git commit是把暂存区的修改提交到master分支。

下面展示的是

chens-MacBook-Pro:learn_git chenchen$  vi readme 
chens-MacBook-Pro:learn_git chenchen$ cat readme
Git is a distributed version control system.
Git is free software distributed under the GPL

add fourth
chens-MacBook-Pro:learn_git chenchen$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   readme

no changes added to commit (use "git add" and/or "git commit -a")
chens-MacBook-Pro:learn_git chenchen$ git add readme 
chens-MacBook-Pro:learn_git chenchen$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   readme

chens-MacBook-Pro:learn_git chenchen$ git commit -m" add fourth"
[master ff3af09]  add fourth
 Committer: chen chen <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 2 insertions(+)
chens-MacBook-Pro:learn_git chenchen$ git status
On branch master
nothing to commit, working tree clean
chens-MacBook-Pro:learn_git chenchen$ 

从上面可以看到不同状态下,git status会输出什么。

比如添加了文件,但是没有git add。或者git add了,但是没有git commit。以及最后git commit了,输出git status会有什么。<

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值