2020-07-24 git 分支操作

查看本地和远端分支命令:
Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (master)

$ git branch 查看本地分支

  • master

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (master)

$ git branch -a 远端分支

  • master
    remotes/origin/master

创建本地分支“

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (master)

$ git checkout -b branch1

Switched to a new branch 'branch1'

提交本地修改到分支

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (master)

$ git checkout -b branch1 检查分支

Switched to a new branch 'branch1'

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (branch1)

$ git status

On branch branch1 发现分支
nothing to commit, working tree clean

测试分支提交:
Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (branch1)
$ vim test.txt

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (branch1)

$ git status

On branch branch1
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)

    modified:   test.txt

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

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (branch1)

$ git add test.txt

warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory.

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (branch1)

$ git status

On branch branch1
Changes to be committed:
(use "git reset HEAD ..." to unstage)

    modified:   test.txt

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (branch1)

$ git commit -m "提叫到分支1上"

[branch1 f00643c] 提叫到分支1上
1 file changed, 3 insertions(+), 1 deletion(-)

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (branch1)

$ git push 推送

fatal: The current branch branch1 has no upstream branch.
To push the current branch and set the remote as upstream, use

git push --set-upstream origin branch1

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (branch1)

$ git push --set-upstream origin branch1

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 312 bytes | 312.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'branch1' on GitHub by visiting:
remote: https://github.com/hanmoxuangithub/AutoTest001/pull/new/branch1
remote:
To github.com:hanmoxuangithub/AutoTest001.git

  • [new branch] branch1 -> branch1
    Branch 'branch1' set up to track remote branch 'branch1' from 'origin'.
图片.png

删除分支:

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (branch1)
$ git branch

  • branch1
    master

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (branch1)
$ git branch -a

  • branch1
    master
    remotes/origin/branch1
    remotes/origin/master

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (branch1)
$ pwd
/d/muke/AutoTest001

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (branch1)
$ git branch -d branch1
error: Cannot delete branch 'branch1' checked out at 'D:/muke/AutoTest001'

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (branch1)
$
要先切换到别的分支才能删除

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (master)

$ git branch -d branch1

warning: deleting branch 'branch1' that has been merged to
'refs/remotes/origin/branch1', but not yet merged to HEAD.
Deleted branch branch1 (was f00643c).

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (master)
$ git branch

  • master

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (master)
$ git branch -a

  • master
    remotes/origin/branch1
    remotes/origin/master 发现远程分支仍然存在

接着删除
Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (master)

$ git branch -r -d origin/branch1

Deleted remote-tracking branch origin/branch1 (was f00643c).

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (master)
$ git branch -a

  • master
    remotes/origin/master

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (master)
$ git branch -r -d origin/branch1
Deleted remote-tracking branch origin/branch1 (was f00643c).

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (master)
$ git branch -a

  • master
    remotes/origin/master

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (master)

$ git push origin :branch1 origin后面有空格

To github.com:hanmoxuangithub/AutoTest001.git

  • [deleted] branch1
图片.png

删除成功!

合并分支:git merge 需要合并的分支

版本回退:
回退到上一个版本
Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (aaa)

$ git reset --hard HEAD^

HEAD is now at f1781a3 增加测试文件
回退到指定版本
Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (aaa)

$ git reset --hard cefaea9

HEAD is now at cefaea9 增加合并内容

先查出版本id 号

Administrator@EDZ-20180311VZF MINGW64 /d/muke/AutoTest001 (aaa)

$ git reflog

f1781a3 (HEAD -> aaa, origin/master) HEAD@{0}: reset: moving to HEAD^
cefaea9 (origin/aaa, master) HEAD@{1}: checkout: moving from master to aaa
cefaea9 (origin/aaa, master) HEAD@{2}: checkout: moving from master to master
cefaea9 (origin/aaa, master) HEAD@{3}: merge aaa: Fast-forward
f1781a3 (HEAD -> aaa, origin/master) HEAD@{4}: checkout: moving from aaa to master
cefaea9 (origin/aaa, master) HEAD@{5}: commit: 增加合并内容
f1781a3 (HEAD -> aaa, origin/master) HEAD@{6}: checkout: moving from master to aaa
f1781a3 (HEAD -> aaa, origin/master) HEAD@{7}: checkout: moving from branch1 to master
f00643c HEAD@{8}: commit: 提叫到分支1上
f1781a3 (HEAD -> aaa, origin/master) HEAD@{9}: checkout: moving from master to branch1
f1781a3 (HEAD -> aaa, origin/master) HEAD@{10}: commit (initial): 增加测试文件

你可能感兴趣的:(2020-07-24 git 分支操作)