1.git 常见操作

新建分支

假设你正在你的项目上工作,想新建一条分支并切换新分支上
git checkout -b 分支名

例如:新建一条dev分支并切换新分支上

运行:
git checkout -b dev

查看分支

git branch

切回master

git checkout master

合并分支

  1. 切换master分支
    git checkout master

  2. 合并分支
    git merge 分支名

如果合并冲突

例子:合并dev 分支

$ git merge dev
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.

Git告诉我们,readme.txt文件存在冲突,必须手动解决冲突后再提交。git status也可以告诉我们冲突的文件:


$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

我们可以直接查看readme.txt的内容:

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1

Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容,我们修改如下后保存:

Creating a new branch is quick and simple.
$ git add readme.txt 
$ git commit -m "conflict fixed"
[master cf810e4] conflict fixed

再提交:

$ git add readme.txt 
$ git commit -m "conflict fixed"
[master cf810e4] conflict fixed

查看分支

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

删除dev分支:

$ git branch -d dev

posted @ 2021-01-22 14:17  鲁哒哒  阅读(44)  评论(0编辑  收藏  举报