Git常用命令remote和branch和checkout

Git常用命令remote和branch和checkout

1、remote

用于在远程仓库的操作。

# 显示所有远程仓库
$ git remote
# 此命令可显示更详细信息
$ git remote -v
# 例子
$ git remote
origin

$ git remote -v
origin  https://gitee.com/zsx242030/um.git (fetch)
origin  https://gitee.com/zsx242030/um.git (push)
# 显示某个远程仓库的信息
$ git remote show remote
# 例子
$ git remote show origin
* remote origin
  Fetch URL: https://gitee.com/zsx242030/um.git
  Push  URL: https://gitee.com/zsx242030/um.git
  HEAD branch: master
  Remote branches:
    branch1  tracked
    branch2  tracked
    branch_a tracked
    branch_b tracked
    branch_c tracked
    develop  tracked
    master   tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)
# 增加一个新的远程仓库,并命名
# 通过该命令将本地的代码库连接到远程服务器
$ git remote add [shortname] [url]
$ git remote add master https://gitee.com/zsx242030/um.git
$ git remote -v
master  https://gitee.com/zsx242030/um.git (fetch)
master  https://gitee.com/zsx242030/um.git (push)
origin  https://gitee.com/zsx242030/um.git (fetch)
origin  https://gitee.com/zsx242030/um.git (push)
# 删除远程仓库的关联
$ git remote rm [shortname]
$ git remote rm master
$ git remote -v
origin  https://gitee.com/zsx242030/um.git (fetch)
origin  https://gitee.com/zsx242030/um.git (push)
# 设置远程仓库的关联
$ git remote set-url origin <newurl>

2、branch

用于列出创建或删除分支。

2.1 创建分支

# 新建一个分支,但依然停留在当前分支
# 在当前分支下新建一个分支,新分支会复制当前分支的内容
# 注意:如果当前分支有修改,但是没有提交到仓库,此时修改的内容是不会被复制到新分支的
$ git branch branch-name
# 新建一个分支,指向指定commit
$ git branch branch-name commit
# 新建一个分支,与指定的远程分支建立追踪关系
$ git branch --track branch-name remote-branch
# 建立追踪关系,在现有分支与指定的远程分支之间
$ git branch --set-upstream branch-name remote-branch

2.2 查看分支

# 显示当前代码库中所有的本地分支
$ git branch 
# 或者是
$ git branch -l
* branch_a
  master
# 列出所有远程分支
$ git branch -r
  origin/HEAD -> origin/master
  origin/branch1
  origin/branch2
  origin/branch_a
  origin/branch_b
  origin/branch_c
  origin/develop
  origin/master
# 列出所有本地分支和远程分支
$ git branch -a
* branch_a
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/branch1
  remotes/origin/branch2
  remotes/origin/branch_a
  remotes/origin/branch_b
  remotes/origin/branch_c
  remotes/origin/develop
  remotes/origin/master
# 查看所有分支并带上最新的提交信息
$ git branch -av
* branch_a                b9709d6 branch_a | update a.txt | add new.txt
  master                  40f466a develop | add test.txt
  remotes/origin/HEAD     -> origin/master
  remotes/origin/branch1  40f466a develop | add test.txt
  remotes/origin/branch2  40f466a develop | add test.txt
  remotes/origin/branch_a b9709d6 branch_a | update a.txt | add new.txt
  remotes/origin/branch_b b1786d3 branch_b | update a.txt | add new.txt
  remotes/origin/branch_c 9fb14a6 branch_c | update a.txt | delete e.txt
  remotes/origin/develop  40f466a develop | add test.txt
  remotes/origin/master   40f466a develop | add test.txt
# 查看本地分支对应的远程分支
$ git branch -vv
* branch_a b9709d6 [origin/branch_a] branch_a | update a.txt | add new.txt
  master   40f466a [origin/master] develop | add test.txt

2.3 删除分支

# 删除本地分支,会阻止删除包含未合并更改的分支
# 如果分支没有被合任然想要合并用大写-D
$ git branch -d branch-name
# 强制删除一个本地分支,即使包含未合并更改的分支
$ git branch -D branchname
# 删除远程分支
$ git branch -dr remote/branch

2.4 修改分支

# 修改当前分支名
$ git branch -m newbranchname
# 本地分支改名
$ git branch -m oldbranchname newbranchname

2.5 分支信息查看

# 显示包含提交50089的分支
$ git branch --contains 50089
# 显示所有已合并到当前分支的分支
$ git branch --merged
# 显示所有未合并到当前分支的分支
$ git branch --no-merged

2.6 多人协作

# 在本地创建和远程分支对应的分支,名称最好一致
$ git checkout -b 分支名 origin/分支名
# 建立本地分支和远程分支的关联
$ git branch --set-upstream-to=origin/分支名 分支名
# 先抓取远程的更新,如果有冲突,手动解决冲突
$ git pull
# 解决冲突后推送
$ git push origin 分支名

因此多人协作工作模式一般是这样的:

首先,可以试图用 git push origin branch-name 推送自己的修改,如果推送失败,则因为远程分支比你的本

地更新早,需要先用 git pull 试图合并。如果合并有冲突,则需要解决冲突,并在本地提交。再用 git push

origin branch-name 推送。

3、checkout

用于切换分支或恢复工作树文件。

3.1 新建和切换分支

# 切换分支(切换分支时,本地工作区,仓库都会相应切换到对应分支的内容)
$ git checkout branchname
# 新建一个分支,并切换到该分支
# 从当前分支创建新分支
$ git checkout -b branchname
# 基于master分支创建一个test分支,并切换到该分支
$ git checkout -b test master
# 新建一条空分支
$ git checkout --orphan emptyBranchName
# 切换到上一个分支
$ git checkout -
# 新建一个分支,指向某个tag
$ git checkout -b branch tag
# 从远程分支develop创建新本地分支devel并检出
$ git checkout -b devel origin/develop
# 检出远程分支origin/develop并创建本地跟踪分支
# 本地分支与远程分支同名
$ git checkout --track origin/develop
touch a.txt
git add a.txt
git commit -m "add a.txt"

git branch branch_a

touch b.txt
git add b.txt
git commit -m "add b.txt"

git log --oneline
500fefe (HEAD -> master) add b.txt
8f5d859 (branch_a) add a.txt

git checkout branch_a
Switched to branch 'branch_a'

git log --oneline
8f5d859 (HEAD -> branch_a) add a.txt

3.2 恢复工作树文件

# 恢复暂存区的指定文件到工作区
$ git checkout file
# 恢复某个commit的指定文件到暂存区和工作区
$ git checkout commit file
# 恢复暂存区的所有文件到工作区
$ git checkout .
# 回退最后一次的提交内容
# 可用于修改错误回退
# 此命令会使用HEAD中的最新内容替换掉你的工作目录中的文件,已添加到暂存区的改动以及新文件都不会受到影响
# --是用来表示后面接的是个路径,不加也可以
$ git checkout HEAD -- file
# 等价
$ git checkout -- file
# 回滚到最近的一次提交
# 如果修改某些文件后,没有提交到暂存区,此时的回滚是回滚到上一次提交
# 如果是已经将修改的文件提交到仓库了,这时再用这个命令回滚无效
$ git checkout HEAD
# 回滚到最近一次提交的上一个版本
$ git checkout HEAD^
# 回滚到最近一次提交的上2个版本
$ git checkout HEAD^^
# 切换到某个指定的commit版本
$ git checkout commit_id
# 切换指定tag
$ git checkout tag
# 检出版本v2.0
git checkout v2.0

在开发的正常阶段,HEAD 一般指向 master 或是其它的本地分支,但当你使用 git checkout 切换到指定的某一次

提交的时候,HEAD 就不再指向一个分支了,它直接指向一个提交,HEAD 就会处于 detached 状态(游离状态)。

$ git log
commit b9709d6e59125cc6d6de086527984cf1cab37c0f (HEAD -> branch_a, origin/branch_a)
Author: zsx242030 <2420309401@qq.com>
Date:   Thu May 18 20:36:54 2023 +0800

    branch_a | update a.txt | add new.txt

切换到某一次提交后,你可以查看文件,编译项目,运行测试,甚至编辑文件而不需要考虑是否会影响项目的当前

状态,你所做的一切都不会被保存到主栈的仓库中。当你想要回到主线继续开发时,使用 git checkout

branchName 回到项目初始的状态,这时候会提示你是否需要新建一条分支用于保留刚才的修改。

$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
        e.txt
Please commit your changes or stash them before you switch branches.
Aborting

哪怕你切换到了某一版本的提交,并且对它做了修改后,不小心提交到了暂存区,只要你切换回分支的时候,依然

会回到项目的初始状态。注意:你所做的修改,如果 commit 了,会被保存到那个版本中。切换完分支后,会提

示你是否要新建一个分支来保存刚才修改的内容。如果你刚才解决了一个 bug ,这时候可以新建一个临时分支,

然后你本地自己的开发主分支去合并它,合并完后删除临时分支。

3.3 恢复修改文件操作

对于恢复修改的文件,就是将文件从仓库中拉到本地工作区,即 仓库区 —> 暂存区 —> 工作区。

对于修改的文件有三种情况:

3.3.1 只是修改了文件,没有任何 git 操作
# 只是修改了文件,没有任何git操作,直接一个命令就可回
# 放弃修改单个文件: git checkout -- <文件名>
# 放弃修改所有文件: git checkout .
$ cat a.txt
branch_a

$ echo a > a.txt

$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

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:   a.txt

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

$ git checkout -- a.txt

$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

nothing to commit, working tree clean

$ cat a.txt
branch_a
3.3.2 修改了文件,并提交到暂存区
# 修改之后,git add但没有git commit
# 放弃指定文件的缓存: git reset HEAD <文件名>
# 放弃全部文件的缓存: git reset HEAD . <=>  git reset HEAD
$ cat a.txt
branch_a

$ echo a > a.txt

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

$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   a.txt

# 回退到当前版本
$ git reset HEAD
Unstaged changes after reset:
M       a.txt

$ git checkout -- a.txt

$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

nothing to commit, working tree clean

$ cat a.txt
branch_a
3.3.3 修改了文件,并提交到仓库区
# 修改之后,git add和git commit -m
$ cat a.txt
branch_a

$ echo a > a.txt

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

$ git commit -m "update a.txt"
[branch_a d0fe7e6] update a.txt
 1 file changed, 1 insertion(+), 1 deletion(-)

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

nothing to commit, working tree clean

# 回退到上一个版本
$ git reset HEAD^
Unstaged changes after reset:
M       a.txt

$ git checkout -- a.txt

$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

nothing to commit, working tree clean

$ cat a.txt
branch_a
# 也可以一步到位直接撤销最后一次的commit
# 回退到上一次commit: git reset --hard HEAD^
# 回退到任意版本: git reset --hard commitid
$ git reset --hard HEAD^
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值