-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
## 简单分支管理 | ||
|
||
1. 查看本地分支 | ||
``` | ||
git branch | ||
``` | ||
2. 查看远程分支 | ||
``` | ||
git branch -r | ||
``` | ||
3. 查看所有分支 | ||
``` | ||
git branch -a | ||
``` | ||
4. 创建本地新分支 | ||
``` | ||
命令如下 | ||
git branch [branch name] | ||
操作示例 | ||
git branch develop2 | ||
``` | ||
5. 切换到新分支 | ||
``` | ||
命令如下 | ||
git checkout [branch name] | ||
操作示例 | ||
git checkout develop2或者switched to branch 'develop2' | ||
``` | ||
6. 创建分支同时也切换分支 | ||
``` | ||
git checkout -b [branch name] | ||
git checkout -b [branch name]的效果相当于以下两步操作: | ||
git branch [branch name] | ||
git checkout [branch name] | ||
``` | ||
7. 推送本地新分支到远程仓库 | ||
命令如下 | ||
``` | ||
git push origin <本地分支名>:<远程分支名> | ||
git push origin [branch name]:[branch name] 或者 git push origin [branch name] | ||
操作示例 | ||
git push origin develop2:develop2或者 git push origin develop2 | ||
``` | ||
|
||
## 常用Git操作 | ||
|
||
1. 创建+切换分支 | ||
创建完分支的同时切换到新创建的分支上: | ||
``` | ||
git checkout -b [branch name] | ||
``` | ||
以上的步骤等同于下面两步: | ||
``` | ||
git branch [branch name] | ||
git checkout [branch name] | ||
``` | ||
2. 推送分支到Git远程仓库 | ||
``` | ||
命令如下 | ||
git push origin [branch name] | ||
操作示例 | ||
git push origin develop2 | ||
``` | ||
3. 删除本地分支 | ||
``` | ||
命令如下 | ||
git branch -d [branch name] | ||
操作示例 | ||
git branch -d develop2 | ||
4. 删除远程分支 | ||
命令如下 | ||
git push origin –delete :[branch name] | ||
操作示例 | ||
git push origin –delete :develop2 | ||
``` | ||
|
||
## 常规提交代码流程 | ||
|
||
1. checkout-切换到新分支 | ||
``` | ||
git checkout [branch name] | ||
``` | ||
2.添加本地需要提交的代码 | ||
``` | ||
git add . | ||
``` | ||
3.提交代码到本地仓库 | ||
``` | ||
git commit -m "提交代码的注释内容" | ||
``` | ||
4.拉取最新代码 | ||
``` | ||
git pull 是在提交到远程仓库之前,先更新到最新版本代码,防止和别人代码产生冲突。 | ||
``` | ||
5.推送本地代码到远程仓库 | ||
``` | ||
git push origin [branch name] | ||
``` | ||
|
||
## 回滚代码 | ||
|
||
1.用命令行打开git项目路径,git log 命令查看commit记录 | ||
``` | ||
git log | ||
``` | ||
2.找到你需要回滚的commitId,输入git reset --hard xxxxxx,将本地文件回滚: | ||
``` | ||
git reset --hard xxxxxx | ||
``` | ||
3.此时本地文件就已回到了commit xxxxxx 之后的状态,但是远程的仓库里并没改变,需要继续远程回滚: | ||
``` | ||
git push -f (这个命令的作用是将自己本地仓库的代码直接推送至仓库,完全以你的提交为准,之前其他人的提交都会被覆盖。) | ||
``` | ||
执行,回滚成功后,继续git log 下,你就会发现第一个就是你刚才操作的commit 123456xxxxxx 就对了。 | ||
查看远程仓库,最新版本已经恢复到历史那个版本。 | ||
|
||
## 保留本地的更改,中止合并 -> 重新合并 -> 重新拉取 | ||
``` | ||
git merge --abort | ||
git reset --merge | ||
``` | ||
|