Git常用命令备忘录

备忘教程

learngitbranching.js.org.

克隆远程仓库项目

1
$ git clone https://gitee.com/marspie/git-training.git

修改提交

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ cd git-training
$ echo "test" >> a.txt
$ git add .
$ git commit -m "test commit"
$ git push -u origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 274 bytes | 137.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Powered by Gitee.com
To https://gitee.com/marspie/git-training.git
83f8711..f49cd75 master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

分支

查看当前分支信息

1
2
3
4
5
6
7
8
$ git branch
* master

$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

创建远程分支

1、新建本地分支

1
2
3
4
5
6
$ git checkout -b dev
Switched to a new branch 'dev'

$ git branch
* dev
master

星号(*)表示当前所在分支。现在的状态是成功创建的新的分支并且已经切换到新分支上。

2、把新建的本地分支push到远程服务器,远程分支与本地分支同名(当然可以随意起名)

1
2
3
4
5
$ git push origin dev:dev
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by Gitee.com
To https://gitee.com/marspie/git-training.git
* [new branch] dev -> dev

3、查看所有分支

1
$ git branch -a

4、切换分支

1
$ git checkout master

删除远程分支

1、推送一个空分支到远程分支,其实就相当于删除远程分支:

1
$ git push origin :dev

2、也可以使用

1
2
3
4
$ git push origin --delete dev
remote: Powered by Gitee.com
To https://gitee.com/marspie/git-training.git
- [deleted] dev

删除本地分支

1
2
$ git branch -d dev
Deleted branch dev (was f49cd75).

分支合并

dev分支的工作成果合并到master分支上

1
2
3
4
5
6
$ git merge dev
Updating f49cd75..b7c3423
Fast-forward
dev.txt | Bin 0 -> 12 bytes
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 dev.txt

Git回退

查看 git 日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ git log -3
commit b9efa3bb30dfe635e40f42ec6a45ada0e06b4712 (HEAD -> dev2)
Author: yaonew <yaonew@126.com>
Date: Thu Nov 29 14:33:25 2018 +0800

dev2 modify

commit a262f949bbd3fe881e6c5ecdc5c0bb2735b0d8c7 (origin/dev2)
Author: yaonew <yaonew@126.com>
Date: Thu Nov 29 14:31:01 2018 +0800

dev2 commit

commit b7c342333b482ea3f3b9c3b0231d26fd322ec326 (origin/dev, dev)
Author: yaonew <yaonew@126.com>
Date: Thu Nov 29 14:19:07 2018 +0800

dev branch

回退到指定版本

1
$ git reset --hard a262f949bbd3fe881e6c5ecdc5

强制提交

1
$ git push -f origin dev2

参考
https://blog.csdn.net/yeputi1015/article/details/80571204
https://www.cnblogs.com/wancy86/p/5848024.html
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001375840038939c291467cc7c747b1810aab2fb8863508000
https://www.cnblogs.com/wangmingshun/p/5425150.html

Alex.Yao wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!