git-02

本文介绍如何使用Git进行远程分支的基本操作,包括与远程分支建立连接、删除分支、查看分支状态、获取远程分支更新及合并分支等内容。

git-02

1、与远程建立连接:

git branch --set-upstream-to=origin/dev(dev关联分支名称):本地分支与远程分支关联
upstream 分支可以定义为我们的本地分支(也称为远程跟踪分支)在远程仓库上遵循的分支。当我们需要将默认远程分支设置为我们当前的本地分支时,运行 git upstream 命令。使用分支名称将本地分支设置为远程分支
git branch --set-upstream
git push -u
git push --set-upstream
git remote add:添加当前本地仓库的远程repo
git push 后不加参数的时候,默认就是git push origin 当前的分支名,比如对本地的master分支执行git push(git push origin master),但远程仓库没有master这个分支的话,肯定会报错

2、删除分支:

  • git branch -d dev :删除本地分支
  • git branch --delete dev : 删除远程分支
  • git push origin --delete Chapater6 删除远程分支

3、查看分支:

  • git branch -r :查看远程分支
  • git branch :查看本地分支

4、git fetch:

FETCH_HEAD: 是一个版本链接,记录在本地的一个文件中,指向目前已经从远程仓库取下来的分支的末端版本。

  • git fetch :更新git remote 中所有的远程repo 所包含分支的最新commit-id, 将其记录到.git/FETCH_HEAD文件中
  • git fetch remote_repo:更新名称为remote_repo 的远程repo上的所有branch的最新commit-id,将其记录
  • git fetch remote_repo remote_branch_name:更新名称为remote_repo 的远程repo上的分支remote_branch_name
  • git fetch remote_repo remote_branch_name:local_branch_name:更新名称为remote_repo 的远程repo上的分支remote_branch_name ,并在本地创建local_branch_name 分支保存远端分支的所有数据。

5、git pull

  • 基于本地的FETCH_HEAD记录,比对本地的FETCH_HEAD记录与远程仓库的版本号,然后git fetch 获得当前指向的远程分支的后续版本的数据,然后再利用git merge将其与本地的当前分支合并。
  • git pull 后不加参数:默认就是git pull origin 当前分支名,远程仓库没有跟本地当前分支名一样的分支的话,肯定会报错。
  • 本地master分支执行git pull的时候等同于git pull origin master。
  • git pull=git fetch + git merge
  • git pull 等效于先执行 git fetch origin 当前分支名, 再执行 git merge FETCH_HEAD.

6、例子

要把远程origin仓库的future分支合并到本地的local分支

  1. 传统方法:
    • git fetch origin future
    • git checkout local
    • git merge FETCH_HEAD
  2. git pull:
    • git pull <remoterepo_name> <branch_name>
    • git checkout local
    • git pull origin future
  3. 本地建立远程分支future
    • git checkout future
    • git pull // 如果本地没有future分支的,这一步都可以不执行。
    • git checkout local
    • git merge future //这一步可以加上 --no-ff 参数,即 git merge --no-ff
fuyu1@ubuntu:~/git-2.7.4$ sudo apt install build-essential libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 gettext 已经是最新的版本。 libssl-dev 已经是最新的版本。 下列软件包是自动安装的并且现在不需要了: git-man liberror-perl Use 'apt-get autoremove' to remove them. 将会安装下列额外的软件包: dpkg-dev fakeroot libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libdpkg-perl libfakeroot libfile-fcntllock-perl 建议安装: debian-keyring libcurl4-doc libcurl3-dbg 下列软件包将被【卸载】: libcurl4-openssl-dev 下列【新】软件包将被安装: build-essential dpkg-dev fakeroot libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libcurl4-gnutls-dev libdpkg-perl libexpat1-dev libfakeroot libfile-fcntllock-perl 升级了 0 个软件包,新安装了 11 个软件包,要卸载 1 个软件包,有 4 个软件包未被升级。 需要下载 1,434 kB 的归档。 解压缩后会消耗 4,975 kB 的额外空间。 您希望继续执行吗? [Y/n] y 获取:1 http://rdsource.tp-link.com/ubuntu/ trusty-updates/main libdpkg-perl all 1.17.5ubuntu5.8 [179 kB] 获取:2 http://rdsource.tp-link.com/ubuntu/ trusty-updates/main dpkg-dev all 1.17.5ubuntu5.8 [726 kB] 获取:3 http://rdsource.tp-link.com/ubuntu/ trusty/main build-essential amd64 11.6ubuntu6 [4,838 B] 获取:4 http://rdsource.tp-link.com/ubuntu/ trusty/main libfakeroot amd64 1.20-3ubuntu2 [25.4 kB] 获取:5 http://rdsource.tp-link.com/ubuntu/ trusty/main fakeroot amd64 1.20-3ubuntu2 [55.0 kB] 获取:6 http://rdsource.tp-link.com/ubuntu/ trusty/main libalgorithm-diff-perl all 1.19.02-3 [50.0 kB] 获取:7 http://rdsource.tp-link.com/ubuntu/ trusty/main libalgorithm-diff-xs-perl amd64 0.04-2build4 [12.6 kB] 获取:8 http://rdsource.tp-link.com/ubuntu/ trusty/main libalgorithm-merge-perl all 0.08-2 [12.7 kB] 获取:9 http://rdsource.tp-link.com/ubuntu/ trusty-updates/main libcurl4-gnutls-dev amd64 7.35.0-1ubuntu2.20 [237 kB] 获取:10 http://rdsource.tp-link.com/ubuntu/ trusty-updates/main libexpat1-dev amd64 2.1.0-4ubuntu1.4 [115 kB] 获取:11 http://rdsource.tp-link.com/ubuntu/ trusty/main libfile-fcntllock-perl amd64 0.14-2build1 [15.9 kB] 已下载 1,434 kB,耗时 2秒 (507 kB/s) (正在读取数据库 ... 系统当前共安装有 106043 个文件和目录。) 正在卸载 libcurl4-openssl-dev:amd64 (7.35.0-1ubuntu2.20) ... 正在处理用于 man-db (2.6.7.1-1ubuntu1) 的触发器 ... 正在选中未选择的软件包 libdpkg-perl。 (正在读取数据库 ... 系统当前共安装有 106022 个文件和目录。) 正准备解包 .../libdpkg-perl_1.17.5ubuntu5.8_all.deb ... 正在解包 libdpkg-perl (1.17.5ubuntu5.8) ... 正在选中未选择的软件包 dpkg-dev。 正准备解包 .../dpkg-dev_1.17.5ubuntu5.8_all.deb ... 正在解包 dpkg-dev (1.17.5ubuntu5.8) ... 正在选中未选择的软件包 build-essential。 正准备解包 .../build-essential_11.6ubuntu6_amd64.deb ... 正在解包 build-essential (11.6ubuntu6) ... 正在选中未选择的软件包 libfakeroot:amd64。 正准备解包 .../libfakeroot_1.20-3ubuntu2_amd64.deb ... 正在解包 libfakeroot:amd64 (1.20-3ubuntu2) ... 正在选中未选择的软件包 fakeroot。 正准备解包 .../fakeroot_1.20-3ubuntu2_amd64.deb ... 正在解包 fakeroot (1.20-3ubuntu2) ... 正在选中未选择的软件包 libalgorithm-diff-perl。 正准备解包 .../libalgorithm-diff-perl_1.19.02-3_all.deb ... 正在解包 libalgorithm-diff-perl (1.19.02-3) ... 正在选中未选择的软件包 libalgorithm-diff-xs-perl。 正准备解包 .../libalgorithm-diff-xs-perl_0.04-2build4_amd64.deb ... 正在解包 libalgorithm-diff-xs-perl (0.04-2build4) ... 正在选中未选择的软件包 libalgorithm-merge-perl。 正准备解包 .../libalgorithm-merge-perl_0.08-2_all.deb ... 正在解包 libalgorithm-merge-perl (0.08-2) ... 正在选中未选择的软件包 libcurl4-gnutls-dev:amd64。 正准备解包 .../libcurl4-gnutls-dev_7.35.0-1ubuntu2.20_amd64.deb ... 正在解包 libcurl4-gnutls-dev:amd64 (7.35.0-1ubuntu2.20) ... 正在选中未选择的软件包 libexpat1-dev:amd64。 正准备解包 .../libexpat1-dev_2.1.0-4ubuntu1.4_amd64.deb ... 正在解包 libexpat1-dev:amd64 (2.1.0-4ubuntu1.4) ... 正在选中未选择的软件包 libfile-fcntllock-perl。 正准备解包 .../libfile-fcntllock-perl_0.14-2build1_amd64.deb ... 正在解包 libfile-fcntllock-perl (0.14-2build1) ... 正在处理用于 man-db (2.6.7.1-1ubuntu1) 的触发器 ... 正在设置 libdpkg-perl (1.17.5ubuntu5.8) ... 正在设置 dpkg-dev (1.17.5ubuntu5.8) ... 正在设置 build-essential (11.6ubuntu6) ... 正在设置 libfakeroot:amd64 (1.20-3ubuntu2) ... 正在设置 fakeroot (1.20-3ubuntu2) ... update-alternatives: using /usr/bin/fakeroot-sysv to provide /usr/bin/fakeroot (fakeroot) in 自动模式 正在设置 libalgorithm-diff-perl (1.19.02-3) ... 正在设置 libalgorithm-diff-xs-perl (0.04-2build4) ... 正在设置 libalgorithm-merge-perl (0.08-2) ... 正在设置 libcurl4-gnutls-dev:amd64 (7.35.0-1ubuntu2.20) ... 正在设置 libexpat1-dev:amd64 (2.1.0-4ubuntu1.4) ... 正在设置 libfile-fcntllock-perl (0.14-2build1) ... fuyu1@ubuntu:~/git-2.7.4$ make prefix=/usr/local all SUBDIR git-gui SUBDIR gitk-git SUBDIR perl SUBDIR templates fuyu1@ubuntu:~/git-2.7.4$ sudo make prefix=/usr/local install SUBDIR git-gui SUBDIR gitk-git SUBDIR perl SUBDIR templates install -d -m 755 '/usr/local/bin' install -d -m 755 '/usr/local/libexec/git-core' install git-credential-store git-daemon git-fast-import git-http-backend git-imap-send git-sh-i18n--envsubst git-shell git-show-index git-upload-pack git-remote-testsvn git-http-fetch git-credential-cache git-credential-cache--daemon git-remote-http git-remote-https git-remote-ftp git-remote-ftps git-bisect git-difftool--helper git-filter-branch git-merge-octopus git-merge-one-file git-merge-resolve git-mergetool git-quiltimport git-rebase git-request-pull git-stash git-submodule git-web--browse git-add--interactive git-difftool git-archimport git-cvsexportcommit git-cvsimport git-cvsserver git-relink git-send-email git-svn git-p4 git-instaweb '/usr/local/libexec/git-core' install -m 644 git-mergetool--lib git-parse-remote git-rebase--am git-rebase--interactive git-rebase--merge git-sh-setup git-sh-i18n '/usr/local/libexec/git-core' install git git-upload-pack git-receive-pack git-upload-archive git-shell git-cvsserver '/usr/local/bin' make -C templates DESTDIR='' install make[1]: 正在进入目录 `/home/fuyu1/git-2.7.4/templates' install -d -m 755 '/usr/local/share/git-core/templates' (cd blt && tar cf - .) | \ (cd '/usr/local/share/git-core/templates' && umask 022 && tar xof -) make[1]:正在离开目录 `/home/fuyu1/git-2.7.4/templates' install -d -m 755 '/usr/local/libexec/git-core/mergetools' install -m 644 mergetools/* '/usr/local/libexec/git-core/mergetools' install -d -m 755 '/usr/local/share/locale' (cd po/build/locale && tar cf - .) | \ (cd '/usr/local/share/locale' && umask 022 && tar xof -) make -C perl prefix='/usr/local' DESTDIR='' install make[1]: 正在进入目录 `/home/fuyu1/git-2.7.4/perl' make[2]: 正在进入目录 `/home/fuyu1/git-2.7.4/perl' Appending installation info to /usr/local/lib/perl/5.18.2/perllocal.pod make[2]:正在离开目录 `/home/fuyu1/git-2.7.4/perl' make[1]:正在离开目录 `/home/fuyu1/git-2.7.4/perl' make -C gitweb install make[1]: 正在进入目录 `/home/fuyu1/git-2.7.4/gitweb' make[2]: 正在进入目录 `/home/fuyu1/git-2.7.4' make[2]: “GIT-VERSION-FILE”是最新的。 make[2]:正在离开目录 `/home/fuyu1/git-2.7.4' install -d -m 755 '/usr/local/share/gitweb' install -m 755 gitweb.cgi '/usr/local/share/gitweb' install -d -m 755 '/usr/local/share/gitweb/static' install -m 644 static/gitweb.js static/gitweb.css static/git-logo.png static/git-favicon.png '/usr/local/share/gitweb/static' make[1]:正在离开目录 `/home/fuyu1/git-2.7.4/gitweb' make -C gitk-git install make[1]: 正在进入目录 `/home/fuyu1/git-2.7.4/gitk-git' install -m 755 gitk-wish '/usr/local/bin'/gitk install -d -m 755 '/usr/local/share/gitk/lib/msgs' install -m 644 po/bg.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/ca.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/de.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/es.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/fr.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/hu.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/it.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/ja.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/pt_br.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/ru.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/sv.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/vi.msg '/usr/local/share/gitk/lib/msgs' && true make[1]:正在离开目录 `/home/fuyu1/git-2.7.4/gitk-git' make -C git-gui gitexecdir='/usr/local/libexec/git-core' install make[1]: 正在进入目录 `/home/fuyu1/git-2.7.4/git-gui' DEST /usr/local/libexec/git-core INSTALL 755 git-gui INSTALL 755 git-gui--askpass LINK git-citool -> git-gui DEST /usr/local/share/git-gui/lib INSTALL 644 tclIndex INSTALL 644 about.tcl INSTALL 644 blame.tcl INSTALL 644 branch_checkout.tcl INSTALL 644 branch_create.tcl INSTALL 644 branch_delete.tcl INSTALL 644 branch_rename.tcl INSTALL 644 branch.tcl INSTALL 644 browser.tcl INSTALL 644 checkout_op.tcl INSTALL 644 choose_font.tcl INSTALL 644 choose_repository.tcl INSTALL 644 choose_rev.tcl INSTALL 644 class.tcl INSTALL 644 commit.tcl INSTALL 644 console.tcl INSTALL 644 database.tcl INSTALL 644 date.tcl INSTALL 644 diff.tcl INSTALL 644 encoding.tcl INSTALL 644 error.tcl INSTALL 644 index.tcl INSTALL 644 line.tcl INSTALL 644 logo.tcl INSTALL 644 merge.tcl INSTALL 644 mergetool.tcl INSTALL 644 option.tcl INSTALL 644 remote_add.tcl INSTALL 644 remote_branch_delete.tcl INSTALL 644 remote.tcl INSTALL 644 search.tcl INSTALL 644 shortcut.tcl INSTALL 644 spellcheck.tcl INSTALL 644 sshkey.tcl INSTALL 644 status_bar.tcl INSTALL 644 themed.tcl INSTALL 644 tools_dlg.tcl INSTALL 644 tools.tcl INSTALL 644 transport.tcl INSTALL 644 win32.tcl INSTALL 644 git-gui.ico INSTALL 644 win32_shortcut.js DEST /usr/local/share/git-gui/lib/msgs INSTALL 644 bg.msg INSTALL 644 de.msg INSTALL 644 el.msg INSTALL 644 fr.msg INSTALL 644 hu.msg INSTALL 644 it.msg INSTALL 644 ja.msg INSTALL 644 nb.msg INSTALL 644 pt_br.msg INSTALL 644 ru.msg INSTALL 644 sv.msg INSTALL 644 vi.msg INSTALL 644 zh_cn.msg make[1]:正在离开目录 `/home/fuyu1/git-2.7.4/git-gui' bindir=$(cd '/usr/local/bin' && pwd) && \ execdir=$(cd '/usr/local/libexec/git-core' && pwd) && \ { test "$bindir/" = "$execdir/" || \ for p in git git-shell git-upload-pack git-cvsserver; do \ rm -f "$execdir/$p" && \ test -z "" && \ ln "$bindir/$p" "$execdir/$p" 2>/dev/null || \ cp "$bindir/$p" "$execdir/$p" || exit; \ done; \ } && \ for p in git-receive-pack git-upload-archive; do \ rm -f "$bindir/$p" && \ test -z "" && \ ln "$bindir/git" "$bindir/$p" 2>/dev/null || \ ln -s "git" "$bindir/$p" 2>/dev/null || \ cp "$bindir/git" "$bindir/$p" || exit; \ done && \ for p in git-add git-am git-annotate git-apply git-archive git-bisect--helper git-blame git-branch git-bundle git-cat-file git-check-attr git-check-ignore git-check-mailmap git-check-ref-format git-checkout-index git-checkout git-clean git-clone git-column git-commit-tree git-commit git-config git-count-objects git-credential git-describe git-diff-files git-diff-index git-diff-tree git-diff git-fast-export git-fetch-pack git-fetch git-fmt-merge-msg git-for-each-ref git-fsck git-gc git-get-tar-commit-id git-grep git-hash-object git-help git-index-pack git-init-db git-interpret-trailers git-log git-ls-files git-ls-remote git-ls-tree git-mailinfo git-mailsplit git-merge git-merge-base git-merge-file git-merge-index git-merge-ours git-merge-recursive git-merge-tree git-mktag git-mktree git-mv git-name-rev git-notes git-pack-objects git-pack-redundant git-pack-refs git-patch-id git-prune-packed git-prune git-pull git-push git-read-tree git-receive-pack git-reflog git-remote git-remote-ext git-remote-fd git-repack git-replace git-rerere git-reset git-rev-list git-rev-parse git-revert git-rm git-send-pack git-shortlog git-show-branch git-show-ref git-stripspace git-submodule--helper git-symbolic-ref git-tag git-unpack-file git-unpack-objects git-update-index git-update-ref git-update-server-info git-upload-archive git-var git-verify-commit git-verify-pack git-verify-tag git-worktree git-write-tree git-cherry git-cherry-pick git-format-patch git-fsck-objects git-init git-merge-subtree git-show git-stage git-status git-whatchanged; do \ rm -f "$execdir/$p" && \ test -z "" && \ ln "$execdir/git" "$execdir/$p" 2>/dev/null || \ ln -s "git" "$execdir/$p" 2>/dev/null || \ cp "$execdir/git" "$execdir/$p" || exit; \ done && \ remote_curl_aliases="git-remote-https git-remote-ftp git-remote-ftps" && \ for p in $remote_curl_aliases; do \ rm -f "$execdir/$p" && \ test -z "" && \ ln "$execdir/git-remote-http" "$execdir/$p" 2>/dev/null || \ ln -s "git-remote-http" "$execdir/$p" 2>/dev/null || \ cp "$execdir/git-remote-http" "$execdir/$p" || exit; \ done && \ ./check_bindir "z$bindir" "z$execdir" "$bindir/git-add" fuyu1@ubuntu:~/git-2.7.4$ git --version -bash: /usr/bin/git: 没有那个文件或目录
09-03
从输出信息来看,编译安装 Git 后,直接运行 `git --version` 报错 `-bash: /usr/bin/git: 没有那个文件或目录`,说明系统仍在尝试调用旧版 Git(默认路径 `/usr/bin/git`),而新编译的 Git 安装在 `/usr/local/bin/git`。以下是解决方案: --- ### **解决方法** 1. **检查新版本路径** 确认 Git 已安装到 `/usr/local/bin`: ```bash ls -l /usr/local/bin/git ``` 如果存在,说明编译安装成功。 2. **更新 `PATH` 环境变量** `/usr/local/bin` 优先级应高于 `/usr/bin`,但可能因 `PATH` 顺序导致问题。检查并调整: ```bash echo $PATH # 临时调整(当前会话生效) export PATH=/usr/local/bin:$PATH # 永久调整(添加到 ~/.bashrc) echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bashrc source ~/.bashrc ``` 3. **验证版本** 重新运行: ```bash git --version ``` 应输出 `git version 2.7.4`。 4. **(可选)卸载旧版 Git** 如果旧版 Git 干扰,可通过包管理器卸载: ```bash sudo apt remove git ``` 或直接删除二进制文件(谨慎操作): ```bash sudo rm -f /usr/bin/git ``` --- ### **原因分析** - 系统默认从 `/usr/bin` 查找命令,而编译安装的 Git 在 `/usr/local/bin`。 - 旧版 Git 可能被部分卸载(如依赖冲突时 `apt` 自动移除了 `libcurl4-openssl-dev`),但残留路径未更新。 --- ### **后续建议** - 使用 `which git` 确认当前调用的路径。 - 如果问题持续,检查是否有其他脚本或配置覆盖了 `PATH`。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值