git 常用命令总结

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

git 常用命令总结

本地仓库普通操作

打开本地git bash,使用如下命令生成ssh公钥和私钥对

ssh-keygen -t rsa -C 'xxx@xxx.com' 然后一路回车(-C 参数是你的邮箱地址)

在项目下新建.gitignore文件,在文件里加入下面忽略文件,及不会上传到服务器:

 log.txt
/dr1

创建版本库

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

【Git】Updates were rejected because the tip of your current branch is behind

刚创建的github版本库,在push代码时遇到以上错误:

有如下几种解决方法:

        1,push前先将远程repository修改pull下来

        $ git pull origin master

        $ git push -u origin master

        2,使用强制push的方法:

        $ git push -u origin master -f 

        这样会使远程修改丢失,一般是不可取的,尤其是多人协作开发的时候。

        3,若不想merge远程和本地修改,可以先创建新的分支:

        $ git branch [name]

        然后push

        $ git push -u origin [name]

在解决问题中遇到的几个问题:

        1,git pull 提示refusing to merge unrelated histories

        是因为两端在做完全不同的提交

        解决方法:git pull --allow-unrelated-histories

        2,git pull 报错:There is no tracking information for the current branch

       

        是因为本地分支和远程分支没有建立联系 (使用git branch -vv 可以查看本地分支和远程分支的关联关系) .根据命令行提示只        需要执行以下命令即可

        解决方法:

        一种是直接指定远程master:git pull origin master

        另外一种方法就是先指定本地master到远程的master,然后再去pull:

                git branch --set-upstream-to=origin/远程分支的名字  本地分支的名字

                git pull

        3,git push 报错GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.

                

        是因为GitHub限制单个文件大小不能超过100M

        解决方法:

        a,初始化Git LFS,任何位置运行一次“git lfs init”进行初始化和安装git-lfs.

        初始化成功后就可以用了. 如果没有安装好, 会显示:

       

        可以通过使用 Homebrew 执行“brew install git-lfs”来安装,安装完成后,你还需要执行“git lfs install”命令

        b,注册文件到LFS,例如要注册tar.gz文件, 可以git lfs track "*.tar.gz",此时将追踪相应文件后缀名符合的文件,要是想只是添加某些文件,指定文件就好了,追踪成功后会生成一个.gitattributes文件,里面有LFS管理的文件类型信息。

       

        c,随后就是一般的提交到github,git add ;git commit -m "comment";;git push origin master;所有修改都提交也可以。

       d, 注:正常情况下,就能成功提交上去不报错了, 要是报错了,很有可能是你的大文件之前commit到本地库了,push时还是按之前的方式来提交到github所以报错;解决办法是撤销上一次commit后,进行再次push.

git init
git config --global user.name "you name"
git config --global user.email "you email"

git add . (新增,修改的文件加入版本库)

git add -A 把新增的修改的,删除的文件都删除


git commit -m 'add files'

git commit -am (commit并且push)

git push
git push -u origin master
git status
git remote -v

git branch --查看有几个版本
git checkout login

git checkout -b login


git commit -am (commit并且push)
git merge <branch> 合并指定分支到当前分支.


原文地址:https://www.cnblogs.com/csj007523/p/12623740.html