1.分支
1.1 查看分支
//查看本地分支
$git branch
当前的分支前有*
//查看远端分支
$ git branch -a
1.2 切换分支
1.2.1 操作命令
//切换分支方法1
$ git checkout feature/brach2
//切换远程分支
$ git checkout -b feature/gitbranchtest
Switched to a new branch 'feature/gitbranchtest'
//本地分支切换为远程分支方法
$ git checkout -b feature/V1.0.1 origin/feature/V1.0.1
1.2.2 如果分支报错1
$ git checkout 'feature/V1.0.1'
error: pathspec 'feature/V1.0.1' did not match any file(s) known to git.
解决办法如下:
1)查看远程分支情况
git branch -a
2)如果看不到新的分支情况则
git fetch origin feature/V1.0.1
3)然后再切换分支
$git checkout feature/V1.0.1
1.3 新建分支
git branch feature/V1.0.1
1.4 删除分支
//删除本地分支
git branch -D V1.0.1
//删除远程分支
git branch -r -d origin/feature/nettest
git push origin:feature/nettest
或者
git push origin --delete feature/netest_p2
删除后发现git branch -a还能查到看远处分支.
需要通过以下命令来删除
$ git remote prune origin
1.5 本地新建分支并推送到gitlab
//本地内容创建新的分支
$ git branch feature/gitbranchtest_V1.0.2
//切换到新创建分支
$ git checkout feature/gitbranchtest_V1.0.2
Switched to branch 'feature/gitbranchtest_V1.0.2'
//不能讲新分支推送到gitlab
$ git push origin feature/gitbranchtest_V1.0.2:feature/gitbranchtest_V1.0.2
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for feature/gitbranchtest_V1.0.2, visit:
remote: http://gitlab.gitlabtest.com/myprojects/myproject/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fgitbranchtest_V1.0.2
remote:
To http://gitlab.gitlabtest.com/myprojects/myproject.git
* [new branch] feature/gitbranchtest_V1.0.2 -> feature/gitbranchtest_V1.0.2
//查看分支已经有了
$ git branch -a
//拉分支也不能拉取
$ git pull;
git remote: Counting objects: 27, done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 27 (delta 0), reused 22 (delta 0)
Unpacking objects: 100% (27/27), done.
From http://gitlab.gitlabtest.com/myprojects/myproject
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> feature/gitbranchtest_V1.0.2
my-pc MINGW64 /d/git_works/gitlab/myproject (feature/gitbranchtest_V1.0.2)
$ git push;
fatal: The current branch feature/gitbranchtest_V1.0.2 has no upstream branch.
To push the current branch and set the remote as upstream, use
//这样操作之后就可以推送成功了
$ git push origin feature/gitbranchtest_V1.0.2:feature/gitbranchtest_V1.0.2
Counting objects: 10, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (10/10), 717 bytes | 0 bytes/s, done.
Total 10 (delta 3), reused 0 (delta 0)
remote:
remote: To create a merge request for feature/gitbranchtest_V1.0.2, visit:
remote: http://gitlab.gitlabtest.com/myprojects/myproject/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fgitbranchtest_V1.0.2
remote:
To http://gitlab.gitlabtest.com/myprojects/myproject.git
feature/gitbranchtest_V1.0.2 -> feature/gitbranchtest_V1.0.2
my-pc MINGW64 /d/git_works/gitlab/myproject (feature/gitbranchtest_V1.0.2)
//这样操作可以拉文件
my-pc MINGW64 /d/git_works/gitlab/myproject (feature/gitbranchtest_V1.0.2)
$ git branch --set-upstream-to=origin/feature/gitbranchtest_V1.0.2 feature/gitbranchtest_V1.0.2
Branch feature/gitbranchtest_V1.0.2 set up to track remote branch feature/gitbranchtest_V1.0.2 from origin.
$git pull
1.6 从远程拉取新的分支
以下是另一个客户端拉取刚才新创建的分支并在本地创建
//用git fetch 之后可以正常的pull和push
my-pc MINGW64 /d/git_works/gitlab_copy/myproject (feature/nettest_p2)
$ git fetch origin feature/gitbranchtest_V1.0.2
remote: Counting objects: 749, done.
remote: Compressing objects: 100% (293/293), done.
remote: Total 749 (delta 208), reused 643 (delta 139)
Receiving objects: 100% (749/749), 226.55 KiB | 0 bytes/s, done.
Resolving deltas: 100% (208/208), done.
From http://gitlab.gitlabtest.com/myprojects/myproject
* branch feature/gitbranchtest_V1.0.2 -> FETCH_HEAD
* [new branch] feature/gitbranchtest_V1.0.2 -> origin/feature/gitbranchtest_V1.0.2
my-pc MINGW64 /d/git_works/gitlab_copy/myproject (feature/nettest_p2)
$ git status;
On branch feature/nettest_p2
Your branch is up-to-date with 'origin/feature/nettest_p2'.
nothing to commit, working tree clean
my-pc MINGW64 /d/git_works/gitlab_copy/myproject (feature/nettest_p2)
$ git checkout feature/gitbranchtest_V1.0.2
Switched to a new branch 'feature/gitbranchtest_V1.0.2'
Branch feature/gitbranchtest_V1.0.2 set up to track remote branch feature/gitbranchtest_V1.0.2 from origin.
my-pc MINGW64 /d/git_works/gitlab_copy/myproject (feature/gitbranchtest_V1.0.2)
$ git status;
On branch feature/gitbranchtest_V1.0.2
Your branch is up-to-date with 'origin/feature/gitbranchtest_V1.0.2'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: web-app/src/test/java/myproject/nettestTest.java
no changes added to commit (use "git add" and/or "git commit -a")
my-pc MINGW64 /d/git_works/gitlab_copy/myproject (feature/gitbranchtest_V1.0.2)
$ git add .;
my-pc MINGW64 /d/git_works/gitlab_copy/myproject (feature/gitbranchtest_V1.0.2)
$ git commit -m '修改测试文件'
[feature/gitbranchtest_V1.0.2 f6e6a9982] 修改测试文件
1 file changed, 1 insertion(+), 1 deletion(-)
my-pc MINGW64 /d/git_works/gitlab_copy/myproject (feature/gitbranchtest_V1.0.2)
$ git push;
Counting objects: 10, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (10/10), 721 bytes | 0 bytes/s, done.
Total 10 (delta 3), reused 0 (delta 0)
remote:
remote: To create a merge request for feature/gitbranchtest_V1.0.2, visit:
remote: http://gitlab.gitlabtest.com/myprojects/myproject/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fgitbranchtest_V1.0.2
remote:
To http://gitlab.gitlabtest.com/myprojects/myproject.git
feature/gitbranchtest_V1.0.2 -> feature/gitbranchtest_V1.0.2
1.7 根据commitId拉新分支
//git checkout -b 分支名称 commitid
git checkout -b newbranch cb8fe6d8202e31a8ce73860d102eabc1a0e360c6
2.日常常用操作
2.1 拉取后提交操作
//1.拉取最新代码
git pull
//2.对比修改文件
$ git diff
//3.添加到暂存
$ git add .
//4.取消本次修改
$ git checkout -- filepath/filename
//3.1 添加后暂存后提交
git commit -m "备注"
//3.2 取消添加暂存
git reset HEAD <file>..." to unstage)
//5. 推送
git push
//$git push --set-upstream origin feature/V1.0
//6.如果远程有本地没有拉倒本地
git fetch
3.标签
//查看当前标签
$git tag;
//模糊匹配
git tag -l 'v1.4.2.*'
//新建标签 -m 是注释
$ git tag -a tagname -m '上线V1.4.2 后';
//删除标签
$ git tag -d 'tagname;
//推送单个tag到远程
$git push origin [tagname]
//推送全部标签到要远程
$git push [origin] --tags
4.合并
//合并之前记得pull最新代码
$ git merge origin/feature/V2.4
合并远程分支
$git merge feature/V2.4
讲本地分支合并到当前分支
$git merge --abort"
git merge之后,如果后悔可以用abort终止
合并完之后还需要推送
5.查看提交历史
$git log
git log 会按提交时间列出所有的更新,最近的更新排在最上面
$ git log -p -2
列出最近两次提交的内容差异.
//-p 选项展开显示每次提交的内容差异,用 -2 则仅显示最近的两次更新