那些常用的git命令

一、本地项目上传git

git remote add origin http://git.xxxxx.git

git add .

git commit -m "提交的啥,描述一下"

git push -u origin master

 

二、第一次push的时候一般会遇到如下报错:

这种错误的主要原因是你的远程仓库的内容有改动但是你本地并没有拉取最新的代码所以会报错

$ git push origin master

To https://github.com/xxxxx.git

 ! [rejected]        master -> master (non-fast-forward)

error: failed to push some refs to 'https://github.com/xxxxx.git'

hint: Updates were rejected because the tip of your current branch is behind

hint: its remote counterpart. Integrate the remote changes (e.g.

hint: 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决方法

首先:

 git pull origin master --allow-unrelated-histories

然后建议做完上一步之后再重新:

git add .

git commit -m '提交的啥,描述一下'

git push origin master

三、git pull 错分支恢复到原来的版本操作

git reflog 

git reset --hard HEAD@{n}

四、git 撤回push (慎用!!!)

1git log 查看提交记录
2git reset --soft <版本号>
重置至指定版本的提交
可通过git log再次查看提交记录,确认是否撤回
注:git reset --soft <版本号>命令只会退回本地提交记录,远程此时还未撤回
3git push <remotename> <commit SHA>:<remotebranchname> --force

  • <remotename> 远程仓库名,默认为origin
  • <commit SHA> 提交的唯一码
  • <remotebranchname> 远程分支名

将指定commit记录push至远程,完成撤回远程push记录

五、git 打标签

1.创建新tag 1.0.1

git tag -a 1.0.1 -m "发布1.0.1版本"

2.提到远程

git push origin 1.0.1

3.删除本地tag

git tag -d 1.0.1

4.删除tag提到远程

git push origin :refs/tags/1.0.1

 

六、在之前的提交记录上打tag

1.先找到要打tag的那次commit

git log --pretty=oneline --abbrev-commit

 

 

2.本地建个分支切到这次提交

命令 git checkout -b  [分支名]  [版本名]

例:git checkout -b demo 9953398

3.创建新tag 

git tag -a 1.1.1 -m "发布1.1.1版本"

4.提到远程

git push origin 1.1.1

七、删除远程分支

git push origin --delete [branchname]

 

再遇到继续补充....

posted @ 2021-06-10 11:57  想飞高的菜鸟  阅读(74)  评论(0编辑  收藏  举报