git 命令总结

2022-07-08  本文已影响0人  ag4kd

图示

基本流程

image.png

文件状态

image.png image.png

简易的命令行入门教程

Git 全局设置

git config --global user.name "binny1024"
git config --global user.email "596928539@qq.com"

创建 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 git@gitee.com:binny1024/git-ionc.git
git push -u origin "master"

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

已有仓库?

cd existing_git_repo
git remote add origin git@gitee.com:binny1024/git-ionc.git
git push -u origin "master"
git remote add 远程仓库的名字 远程仓库的地址

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

Git配置解析

user.email=chenhh@xxx.com
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 push <shortname> <本地分支名>:<远程分支名>

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

git push origin master

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

git push origin :master

等同于

git push origin --delete master

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

git push origin

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

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 fetch

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

git fetch remote_repo
git fetch remote_repo remote_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 git@gitee.com:binny1024/git-ionc.git

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

git push ionc master:master

cherry-pick

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关联的新的分支了。

git reflog
git cherry-pick commit-id

移除 暂存区文件

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

附录

git 关联远程仓库

添加远端仓库

git remote add [name_of_repo] [link_of_git]

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

拉回代码

git fetch [name_of_repo]

建立本地分支

git checkout -b [name_of_branch_local] [name_of_repo]/[branch_of_repo_remote]

问题

prior sync failed; rebase still in progress

image.png
上一篇 下一篇

猜你喜欢

热点阅读