继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Git常用命令总结及一些问题思考

拉丁的传说
关注TA
已关注
手记 448
粉丝 126
获赞 788

感谢廖雪峰大神分享的Git课程

欢迎访问
我的博客
GitHub

掌握以下命令,基本上日常够用

git 常用命令总结

本地仓库普通操作

创建版本库

git init

添加文件到缓存区

git add file

添加到仓库

git commit -m "commint describtion"

撤销修改或清除缓存区

git reset /git reset HEAD file

丢弃或撤销修改

git checkout -- file

删除

git rm file 
相当于git rm后git add

撤销删除操作

git reset 
git checkout --file

查看指定文件

git cat file

比对工作区与版本库

 git diff

查看日志

git log

操作历史

git reflog

版本回退

git reset --hard commit-id

或 git reset --hard HEAD-id相当于指定当前HEAD到指定版本

创建并指向分支

git checkout -b <branch>

切换分支

git checkout <branch>

查看分支信息

git branch 
详细信息 git branch -v

合并指定分支到当前分支

git merge <branch>

普通模式合并,保留分支信息

git merge --no-ff -m "" <branch> 此信息会在远程显示

删除分支

git branch -d <branch>

临时保存工作区现场

git stash

列出工作区现场

git list

删除现场并恢复工作区

git stash pop

添加当前工作区全部文件到缓存区

git add .

测试连接

ssh -T git@github.com

远程库与本地库相关

添加远程版本库

git remote add origin git@github.com:路径/版本库.git

克隆远程分支库

git clone git@server:path/repo.git

本地建立远程库对应分支

git checkout -b branch origin/branch

从远程库拉取最新提交

git pull

关联并推送本地版本库到远程库

git push -u origin master

本地分支推送到远程库

git push

查看远程仓库状态

git remote show origin
git remote -v

本地分支与远程分支比对

git diff <branch> origin/<branch>

强制远程库覆盖本地

git fetch --all 
git reset --hard origin/master 
git pull

更新远程信息

git remote update

追踪远程分支

git fetch origin branch-name

建立本地与远程分支的连接

git branch --set-upstream-to <branch> origin/<branch>

克隆指定分支

git clone -b <branch> git@server-name:path/<repo-name>.git

合并冲突

手动解决冲突之后 冲突文件-->git->Resolve Conflicts

实战问题及解决方式总结

commit 后无提交说明遇到的问题:Vim: Caught deadly signal SEGV,无法继续操作git
?只能重启git窗口?
退出log界面:q键退出

删除部分log?
git reset --hard commit-id: HEAD指向commit-id版本,log只显示当前HEAD版本 ,回退全部后仍可显示全部log

删除全部log?待研究

查看帮助:git --help ;指定命令帮助 git log -help;

git reset --hard HEAD-id:回退到指定HEAD版本,id可通过git reflog查看

版本回退:

git reset HEAD^

无--hard时:
Unstaged changes after reset: M <file>;

有--hard时:
完整命令: git reset --hard HEAD^
HEAD is now at <commit-id>

结果HEAD都指向指定版本

未commit的:
checkout -- <file>提示文件匹配不到,需reset 或rest HEAD <file> 清除缓存区之后才能撤销,-f强制删除 无法撤销

已commit的:
reset后checkout也无法撤销,想撤销只能reset --hard commit-id或reset --hard HEAD回退到提交删除之前,无法回退指定文件

添加远程仓库时提示:fatal: remote origin already exists.

进入vi:

vi 
.git/config删除remote “origin”

退出vi编辑器:

ESC后 shift+zz保存 退出

远程库与本地库操作相关问题及总结

版本库内文件,同一路径下的多个分支共享,分支提交,合并到主线

远程库默认名称根据我们初次连接远程库时创建的为准

git pull 提示

there is no tracking information for the current branch说明本地分支和远程分支的连接关系没有创建

git pull提示
error: The following untracked working tree files would be overwritten by merge:.....

Please move or remove them before you can merge.

Aborting

git clean -d -fx ""
其中
x -----删除忽略文件已经对git来说不识别的文件
d -----删除未被添加到git的路径中的文件
f -----强制运行

本地与远程操作流程

进入指定目录后
若无仓库时初始化仓库
关联远程仓库:git remote add origin git@server-name:path/<repo-name>.git
查看远程仓库信息:
git remote -v

两种情况:

未clone过远程分支时:

git remote show origin

git remote update

git pull

以上三个命令提示:

GitLab: The project you were looking for could not be found.

fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

checkout -b <branch> <origin>/<branch>时:

提示:
fatal: 'origin/branch' is not a commit and a branch 'branch' cannot be created from it

checkout <branch> <origin>/<branch>

提示:
error: pathspec 'branch' did not match any file(s) known to git.

error: pathspec 'origin/branch' did not match any file(s) known to git.

此时只能克隆 
git clone git@server-name:path/<repo-name>.git 获取仓库再操作

待研究验证

已克隆过远程分支时:

已建立本地仓库与远程仓库对应连接,默认为master分支,需要切换到已clone的仓库代码路径,继续操作

git remote show origin 提示 tracked 
git remote update 
git pull 无最新提交提示already up-to-date
git fetch
以上命令均可正常操作

后续操作

建立并切换到与远程库对应本地分支 
git checkout -b branch origin/branch
变更已clone过的master分支代码为指定分支代码

git checkout master 切换到master分支,代码重下载

分支切换过程工作区代码会跟随分支变动

git status 查看仓库状态,未修改工作区情况下仓库状态不变 working tree clean

追踪远程库
git fetch origin branch 
拉取最新提交git pull origin branch指定分支或git pull默认当前对应分支
建立连接git branch --set-upstream-to <branch> origin/<branch>

问题:遇到过 git pull 及git remote update 不拉取提示new 且未追踪,待复现

注意:实际开发中需要

切换分支之前需commit工作区到本地分支仓库,提交之后再切换分支,仓库代码将会有所区别

git stash临时保存工作区后,需记住stash id,再切换分支操作,切回远分支后通过git

stash popstash apply <stashid>恢复原工作区

谨记:

本地修改分支:用于本地修改

远程同步分支:用于pull及push;本地修改分支合并到该分支,再push

切换分支前提交或临时保存本地修改分支修改内容到本地仓库,以便切回该分支继续修改

日常git 代码操作流程

本地仓库已与远程仓库关联后:

查看远程仓库信息 git remote show origin
追踪最新提交git fetch origin branch
拉取最新提交 git pull

本地仓库未与远程关联时:

关联本地仓库到远程仓库,
clone远程分支,
git fetch追踪,
git pull拉取
git push

提示:
fatal: The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use

`git push origin HEAD:<branch>`

`To push to the branch of the same name on the remote, use

git push origin <branch>`

完整提交流程

git stash保存本地工作区

git checkout <远程branch>切换到远程分支

git pull拉取远程提交

git checkout <本地branch>切回本地

git merge --no-ff -m "" <远程branch>远程分支合并到本地,解决冲突

git checkout <远程branch>

git merge --no-ff -m "" <本地branch>本地分支合并到远程

git push origin HEAD:<branch>git push

原文链接:http://www.apkbus.com/blog-35555-76818.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP