git常用命令

git常用命令

    • 1. 克隆远程仓库
    • 2. 提交代码到远程仓库
    • 3. 查看分支
    • 4. 切换分支
    • 5. 新建分支
      • 新建分支
      • 新建空白分支
    • 6. 删除分支
      • 删除远程分支
      • 删除本地分支
    • 7. 回滚
      • 回滚到上一个版本
      • 回滚到指定版本

1. 克隆远程仓库

git clone https:xxx.git

2. 提交代码到远程仓库

git add .
git commit -m “description”
git push -u origin 分支名

3. 查看分支

git branch -a

4. 切换分支

git checkout 分支名

5. 新建分支

新建分支

git checkout -b 新分支名
git add .
git commit -m “init”
git push -u origin 新分支名

新建空白分支

git checkout -b 新分支名
git rm -rf .
git clean -f -d
git add .
git commit -m “init”
git push -u origin 新分支名

6. 删除分支

删除远程分支

git push origin --delete 分支名

删除本地分支

git branch -d 分支名

不能在要删除的分支上删除该分支,需要先切换到别的分支在删除;
如果要删除的分支没有merge,则会出现提醒无法删除,此时需要进行如下命令强制删除

git branch -D 分支名

7. 回滚

回滚到上一个版本

git reset --hard HEAD^
本地仓库发生改变,远程仓库还没有发生变化
git push -f
远程仓库也回退了

回滚到指定版本

  1. 查看所有提交记录

git log

  1. 查看最新3次的提交记录

git log -3

步骤1或者2完成后会出现commitID,根据提交信息找到对应的commitID,执行以下命令就能回滚到指定版本:

git reset --hard commitID

最后将远程仓库同步回退:

git push -f

你可能感兴趣的:(git学习笔记)