git 命令总结

图示

基本流程

image.png

文件状态

image.png
image.png
  • 工作区:就是你本地实际写代码的地方,无论你是用 vim 直接改也好,还是在 IDE 里写,都无所谓。
    • 对应的文件状态是:modified,已修改,但还没保存到数据库中。
  • 暂存区:就是临时存放的地方。
    • 对应的文件状态是:staged,Git 已经对该文件做了标记,下次提交知道要包含它。
  • 本地库:存放本地历史版本信息。
    • 对应的文件状态是:committed,文件已经安全的保存在本地数据库中。

简易的命令行入门教程

Git 全局设置

git config --global user.name "binny1024"
git config --global user.email "[email protected]"

创建 git 仓库:

mkdir git-ionc
cd git-ionc
git init 
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin [email protected]:binny1024/git-ionc.git
git push -u origin "master"
  • 建立默认分支

执行了 commit 之后,会自动创建一个名为 master 的分支。

已有仓库?

cd existing_git_repo
git remote add origin [email protected]:binny1024/git-ionc.git
git push -u origin "master"
  • 本地仓库关联远程仓库
    • origin : 在本地自定义的远程仓库的名字。
    • [email protected]:binny1024/git-ionc.git:远程仓库的地址。
git remote add 远程仓库的名字 远程仓库的地址

这条命令可以理解为建立一个虚拟映射:名字 --> 地址

Git配置解析

[email protected]
user.name=chenhh
core.ignorecase=false            # 不许忽略文件名大小写
core.autocrlf=input              # 换行模式为 input,即提交时转换为LF,检出时不转换
core.filemode=false              # 不检查文件权限
core.safecrlf=true               # 拒绝提交包含混合换行符的文件
core.editor=vim
core.repositoryformatversion=0   # Internal variable identifying the repository format and layout version
core.bare=false                  # 默认不创建裸仓库
core.logallrefupdates=true       # log 所有 ref 的更新
core.precomposeunicode=true      # Mac专用选项,开启以便文件名兼容其他系统
push.default=simple                    # 只推送本地当前分支,且与上游分支名字一致
alias.lg=log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
pull.rebase=true                 # 强制开启 rebase 模式
credential.helper store          # 记住密码

git remote

git remote add  
  • shortname : 远程仓库地址的别名
  • url:远程仓库的地址

git pull

git pull <远程主机名> <远程分支名>:<本地分支名>

git push

  • 命令用于将本地分支的更新,推送到远程主机。它的格式与git pull命令相仿。
git push  <本地分支名>:<远程分支名>

注意:这里的:前后是必须没有空格的。

  • 如果省略远程分支名,则表示将本地分支推送与之存在"追踪关系"的远程分支(通常两者同名),如果该远程分支不存在,则会被新建。
git push origin master

上面命令表示,将本地的master分支推送到origin主机的master分支。如果后者不存在,则会被新建。

  • 如果省略本地分支名,则表示删除指定的远程分支,因为这等同于推送一个空的本地分支到远程分支(慎用!删除远程仓库的分支).
git push origin :master

等同于

git push origin --delete master

上面命令表示删除origin主机的master分支。

  • 如果当前分支与远程分支之间存在追踪关系,则本地分支和远程分支都可以省略。
git push origin

上面命令表示,将当前分支推送到origin主机的对应分支。

  • 如果当前分支只有一个追踪分支,那么主机名都可以省略。
git push
  • 如果当前分支与多个主机存在追踪关系,则可以使用-u选项指定一个默认主机,这样后面就可以不加任何参数使用git push。
git push -u origin master

上面命令将本地的master分支推送到origin主机,同时指定origin为默认主机,后面就可以不加任何参数使用git push了。

git fetch 四种基本用法

image.png

git fetch 这个操作是将远程库的数据下载到本地库,但是工作区中的文件没有更新。

什么是FETCH_HEAD?

某个branch在服务器上的最新状态。每一个执行过fetch操作的项目,都会存在一个FETCH_HEAD列表, 这个列表保存在 .git/FETCH_HEAD 文件中, 其中每一行对应于远程服务器的一个分支. 当前分支指向的FETCH_HEAD, 就是这个文件第一行对应的那个分支。
(引用自https://ruby-china.org/topics/4768)

  • 更新git remote 中所有的远程repo 所包含分支的最新commit-id, 将其记录到.git/FETCH_HEAD文件中
git fetch

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

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

远程仓库相关命令

删除远程仓库

git remote rm 远程仓库的名字
git remote rename old_name new_name

操作流程记录

 git config --global core.editor vim
mkdir gitdemo
git init
echo "git 操作日志" > README.md
git add .
git commit -m 'first add readme'

这条命令结束后,会在本地新建一个默认的名为master的分支。

在执行完 上述 简易命令之后,有执行了一次下面的命令:

git remote add ionc [email protected]:binny1024/git-ionc.git

目的是将本地仓库关联两个远程仓库,该命令的实质是将远程仓库简写的名字记录到本地,接着执行下面的命令:

git push ionc master:master

cherry-pick

  • 先是git clone 了一个开源仓库,记为 origin,然后将该开源仓库fork。

  • 在本地修改那个那个开源仓库,然后新建了一个分支 modify ,将该笔改动提交到此分支。

  • 执行下面命令:

git remote add ionc 远程仓库地址
  • 这个时候,我想在新建一个分支使用下面的命令
git checkout -b mine ionc/master
image.png

fatal: 'ionc/master' is not a commit and a branch 'mine' cannot be created from it

上述错误说 ionc/master不是一笔提交,不能从此创建分支。不是很清楚创建分支的内在逻辑。想到一个命令:git fetch ionc,执行此命令之后,再次执行创建分支命令,就可以创建一个与远程仓库ionc关联的新的分支了。

  • 由于我没有权限提交到开源仓库,但是可以通过pr的方式提交审核。我需要把之前在modify上的改动挪到mine分支上,使用了 cherry-pick 命令,先使用 git reflog获取那笔改动的commit ID。(忘记在git fetch ionc之后试一下可不可以直接将modify提到 ionc仓库了。。。下次试一下)
git reflog
git cherry-pick commit-id
  • 这样将在modifty的那笔提交腾挪到mine分支上了。

移除 暂存区文件

git rm --cached -r 文件夹
git rm --cached 文件

将文件夹移除版本控制

  • xxx为文件夹名 ,记得加引号
git rm -r --cached  "xxx/"
  • 提交
git commit -m" remove bin folder all file out of control"
  • 提交到服务器,具体哪个分支自己决定
git push origin master(dev)

附录

git 关联远程仓库

添加远端仓库

git remote add [name_of_repo] [link_of_git]

然后可以通过git remote -v查看添加的仓库。

  • name_of_repo:远程仓库的名字
  • link_of_git:远程仓库的地址

拉回代码

git fetch [name_of_repo]

建立本地分支

git checkout -b [name_of_branch_local] [name_of_repo]/[branch_of_repo_remote]
  • name_of_branch_local:本地分支的名字
  • branch_of_repo_remote:远程仓库的名字

问题

prior sync failed; rebase still in progress

image.png

你可能感兴趣的:(git 命令总结)