开发工具使用 November 05, 2020

Git常用命令及技巧

Words count 3.9k Reading time 4 mins. Read count 0

1 git stash

应用场景:当修改当前分支文件(非新建),且要切换到其它分支时,系统会提示如下信息,这时需要缓存当前分支更改的内容,然后才可以切换分支。

1.1 git stash save

缓存当前更改的内容

git stash save -a "change1"
Saved working directory and index state On master: change1

1.2 git stash list

查看所有的缓存

git stash list
shirukaideMacBook-Pro:git-test shirukai$ git stash list
stash@{0}: On master: change1

1.3 git stash pop

还原缓存

git stash pop 0

1.4 git stash drop

删除某个缓存

git stash drop 0
shirukaideMacBook-Pro:git-test shirukai$ git stash drop 0
Dropped refs/stash@{0} (d4006767bed1a93250dc3420b4619eeb3f1ad088)

1.5 git stash clear

删除所有缓存

git stash clear

2 添加新的远程仓库

git init
git remote add origin 远程仓库地址xxxxx.git
git add .
git commit
git push -u origin master

3 拉取远程仓库的的分支(本地不存在的分支)

git checkout -b 本地分支名 origin/远程分支名

4 删除分支

4.1 删除一个不是当前打开的分支

git branch -d 分支名称

4.2 删除一个正在使用的分支

git branch -D 分支名称

5 打包

git archive tag名称 --format 压缩格式  > 压缩包名称

https://www.cnblogs.com/utank/p/7880441.html

6 操作tag

1 创建tag

git tag -a tag名称 -m “描述”

2 删除tag

git tag -d tag名称

3 推送tag到远程

git push origin tag名称

7 指定分支或tag clone

git clone -b 分支名称/tag名称 仓库名称

8 撤回commit

git reset --soft HEAD^

9 开发错分支

开发中,没注意开发错分支了,比如正常需要在develop分支开发,结果在test分支开发了,这时需要切换到develop分支,并且想将自己已经更改的代码同步到develop分支。参考https://www.cnblogs.com/lovemomo/p/10755483.html

 git add .      (把所有改动暂存)
 git stash     (把暂存的文件提交到git的暂存栈)
 git checkout 本该提交代码的分支 
 git stash pop (将暂存栈中的代码放出来)
0%