Git常用命令

文章目录

1、git init

进入项目根目录后,执行git init

chengwen@chengwen cloud2020 % git init
Initialized empty Git repository in /Users/chengwen/GitHub/cloud2020/.git/

通过ls -la 可以查看隐藏目录.git

chengwen@chengwen cloud2020 % ls -la
total 48
drwxr-xr-x  28 chengwen  staff   896 Apr  8 21:07 .
drwxr-xr-x   6 chengwen  staff   192 Mar 18 00:15 ..
-rw-r--r--@  1 chengwen  staff  8196 Mar  5 01:55 .DS_Store
drwxr-xr-x   9 chengwen  staff   288 Apr  8 21:07 .git
drwxr-xr-x   7 chengwen  staff   224 Mar 18 00:15 .idea
drwxr-xr-x   6 chengwen  staff   192 Mar  6 20:19 cloud-api-commons
drwxr-xr-x   6 chengwen  staff   192 Mar 14 18:37 cloud-config-center3344
drwxr-xr-x   6 chengwen  staff   192 Mar 14 23:01 cloud-config-client3355
drwxr-xr-x   6 chengwen  staff   192 Mar  6 23:33 cloud-consumer-consul-order80
-rw-r--r--   1 chengwen  staff    80 Mar  2 23:39 cloud2020.iml
drwxr-xr-x   5 chengwen  staff   160 Mar 18 00:04 cloudalibaba-provider-payment9001
-rw-r--r--   1 chengwen  staff  5079 Mar 17 23:47 pom.xml

进入.git目录可以看到git的初始文件

该目录.git下存放的是本地库相关的子目录和文件,该目录下的文件不要轻易改动或删除

chengwen@chengwen .git % ls -l
total 24
-rw-r--r--   1 chengwen  staff   23 Apr  8 21:07 HEAD
-rw-r--r--   1 chengwen  staff  137 Apr  8 21:07 config
-rw-r--r--   1 chengwen  staff   73 Apr  8 21:07 description
drwxr-xr-x  14 chengwen  staff  448 Apr  8 21:07 hooks
drwxr-xr-x   3 chengwen  staff   96 Apr  8 21:07 info
drwxr-xr-x   4 chengwen  staff  128 Apr  8 21:07 objects
drwxr-xr-x   4 chengwen  staff  128 Apr  8 21:07 refs
chengwen@chengwen .git % pwd
/Users/chengwen/GitHub/cloud2020/.git

设置签名

  • 形式
    用户名:zhoumin
    email地址:zhoumin@163.com

  • 作用
    区分不同的开发人员的身份

  • 辨析
    这里设置的签名和登录远程库(代码托管中心 如:github)的账户、密码没有 任何关系。

  • 命令
    1、项目级别、仓库级别:仅在当前本地库范围内有效

git config user.name zhoumin
git config user.email zhoumin@163.com

信息保存的位置

chengwen@chengwen .git % cat config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[user]
	name = zhoumin
	email = zhoumin@163.com
chengwen@chengwen .git % pwd
/Users/chengwen/GitHub/cloud2020/.git

2、系统用户级别:登录当前操作系统的用户范围

git config --global user.name zhouminlobal
git config --global user.email zhouminlobal@163.com

信息保存的位置用户的home目录~

chengwen@chengwen ~ % cat .gitconfig
[filter "lfs"]
	clean = git-lfs clean -- %f
	smudge = git-lfs smudge -- %f
	process = git-lfs filter-process
	required = true
[user]
	name = zhouminlobal
	email = zhouminlobal@163.com
chengwen@chengwen ~ % pwd
/Users/chengwen
chengwen@chengwen ~ % 

3、级别优先级
就近原则,项目级别优先于系统用户级别,二者都有时采用项目级别的签名
如果只有系统用户级别的签名,就以系统用户级别的签名为准
二者都没有时不允许

实际操作时设置全局的系统级别的即可

2、查看工作区、暂存区的状态git status

chengwen@chengwen cloud2020 % git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.DS_Store
	.idea/
	cloud-api-commons/
	cloud-config-center3344/
	cloud-config-client3355/
	cloud-consumer-consul-order80/
	cloud-consumer-feign-hystrix-order80/
	cloud-consumer-feign-order80/
	cloud-consumer-hystrix-dashboard9001/
	cloud-consumer-order80/
	cloud-consumerzk-order80/
	cloud-eureka-server7001/
	cloud-eureka-server7002/
	cloud-eureka-server7003/
	cloud-gateway-gateway9527/
	cloud-provider-consul-payment8006/
	cloud-provider-hystrix-payment8001/
	cloud-provider-payment8001/
	cloud-provider-payment8002/
	cloud-provider-payment8004/
	cloud-stream-rabbitmq-consumer8802/
	cloud-stream-rabbitmq-provider8801/
	cloud2020.iml
	cloudalibaba-provider-payment9001/
	pom.xml

nothing added to commit but untracked files present (use "git add" to track)

3、将工作区新建、修改添加到暂存区git add [file name]

chengwen@chengwen cloud2020 % git add .
chengwen@chengwen cloud2020 % 

提交之后再次查看状态

chengwen@chengwen cloud2020 % git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   .DS_Store
	new file:   .idea/compiler.xml
	new file:   .idea/encodings.xml
	new file:   .idea/misc.xml
	new file:   .idea/uiDesigner.xml
	new file:   .idea/workspace.xml
	new file:   cloud-api-commons/cloud-api-commons.iml
	new file:   cloud-api-commons/pom.xml
	new file:   cloud-api-commons/src/main/java/com/ccb/springcloud/entities/CommonResult.java
	new file:   cloud-api-commons/src/main/java/com/ccb/springcloud/entities/Payment.java
	new file:   cloud-api-commons/target/classes/com/ccb/springcloud/entities/CommonResult.class
	new file:   cloud-api-commons/target/classes/com/ccb/springcloud/entities/Payment.class
	new file:   cloud-api-commons/target/cloud-api-commons-1.0-SNAPSHOT.jar
	new file:   cloud-api-commons/target/maven-archiver/pom.properties

4、将暂存区的代码删除git rm -r --cached [file name]

git rm -r --cached .
rm '.DS_Store'
rm '.idea/compiler.xml'
rm '.idea/encodings.xml'
rm '.idea/misc.xml'
rm '.idea/uiDesigner.xml'
rm '.idea/workspace.xml'
rm 'cloud-api-commons/cloud-api-commons.iml'
rm 'cloud-api-commons/pom.xml'
rm 'cloud-api-commons/src/main/java/com/ccb/springcloud/entities/CommonResult.java'
rm 'cloud-api-commons/src/main/java/com/ccb/springcloud/entities/Payment.java'
rm 'cloud-api-commons/target/classes/com/ccb/springcloud/entities/CommonResult.class'
rm 'cloud-api-commons/target/classes/com/ccb/springcloud/entities/Payment.class'

5、将暂存区提交到本地库git commit [file name]

git commit .
[master (root-commit) 6657a48] springcloud2021 commit new file
 245 files changed, 4197 insertions(+)
 create mode 100644 .DS_Store
 create mode 100644 .idea/compiler.xml
 create mode 100644 .idea/encodings.xml
 create mode 100644 .idea/misc.xml
 create mode 100644 .idea/uiDesigner.xml
 create mode 100644 .idea/workspace.xml
 create mode 100644 cloud-api-commons/cloud-api-commons.iml
 create mode 100644 cloud-api-commons/pom.xml
 create mode 100644 cloud-api-commons/src/main/java/com/ccb/springcloud/entities/CommonResult.java
 create mode 100644 cloud-api-commons/src/main/java/com/ccb/springcloud/entities/Payment.java
 create mode 100644 cloud-api-commons/target/classes/com/ccb/springcloud/entities/CommonResult.class
 create mode 100644 cloud-api-commons/target/classes/com/ccb/springcloud/entities/Payment.class

6、将暂存区的内容提交到本地库git commit -m “commit message” [file name]

先看git status,提示变化为git add to update
被修改后的文件pom.xml为红色

chengwen@chengwen cloud2020 % 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:   pom.xml

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

git add pom.xml 从工作区提交被修改的文件到暂存区后,再看被修改的文件pom.xml变为绿色

chengwen@chengwen cloud2020 % git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   pom.xml

chengwen@chengwen cloud2020 % 

git commit -m “second commit modify pom.xml” pom.xml 从暂存区提交修改后的文件到本地库,双引号内的内容为提交注释

chengwen@chengwen cloud2020 % git commit -m "second commit modify pom.xml" pom.xml  
[master 4b5c6ba] second commit modify pom.xml
 1 file changed, 2 insertions(+), 1 deletion(-)
chengwen@chengwen cloud2020 % 

7、显示提交日志 git log

chengwen@chengwen cloud2020 % git log
commit 4b5c6ba363490b0165d89c05994a44d941be28aa (HEAD -> master)
Author: chengwen <chengwen@163.com>
Date:   Thu Apr 8 23:01:19 2021 +0800

    second commit modify pom.xml

commit 6657a482598bb1a42898f6ab7e08172a3e8f28cf
Author: chengwen <chengwen@163.com>
Date:   Thu Apr 8 22:38:12 2021 +0800

    springcloud2021 commit new file
chengwen@chengwen cloud2020 % 

7.1、只显示在一行 git log --pretty=oneline

chengwen@chengwen cloud2020 % git log --pretty=oneline
4b5c6ba363490b0165d89c05994a44d941be28aa (HEAD -> master) second commit modify pom.xml
6657a482598bb1a42898f6ab7e08172a3e8f28cf springcloud2021 commit new file
chengwen@chengwen cloud2020 % 

7.2、只显示一行日志 git log --oneline

chengwen@chengwen cloud2020 % git log --oneline
4b5c6ba (HEAD -> master) second commit modify pom.xml
6657a48 springcloud2021 commit new file
chengwen@chengwen cloud2020 % 

7.3、git reflog

chengwen@chengwen cloud2020 % git reflog
4b5c6ba (HEAD -> master) HEAD@{0}: commit: second commit modify pom.xml
6657a48 HEAD@{1}: 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值