快捷搜索: 王者荣耀 脱发

Git命令操作【全系列】

Git常用命令操作

1 基础命令

①git config --global user.name [‘你的用户名’]:查看/设置

git config --global user.name ziyi:设置用户名为ziyi git config --global user.name:查看用户名

②git config --global user.email [‘你的邮箱’]:查看/设置

设置邮箱

③git reset HEAD test.txt 取消已经缓存的内容

取消test.txt已经缓存的内容

④git rm --cached <file> 将文件从暂存区删除

⑤git branch 相关命令

git branch : 查看分支
git branch <branchName> :创建分支
git checkout <branchName>:切换分支
git merge <branchName>: 将branchName内容合并到当前分支
git branch -d <branchName>:删除分支

⑥git log相关命令

git log -graph:查看历史中什么时候出现分支、合并
git log --reverse:逆向输出日志
git log --author=ziyi --online -5:简洁版本查看ziyi最近的五次提交

⑦git tag相关命令

git tag:查看所有标签
git tag -a -m “某某标签”:打标签

2 进阶命令

2.1 fork别人仓库并同步保持更新

把fork的项目克隆到本地仓库中 Configuring a remote for a fork Syncing a fork
例如:我fork了一份https://github.com/simplezhli/flutter_deer.git(下文叫做A)到我自己的仓库(下文叫做B)

①git clone B(自己的仓库)

git clone 我自己fork的大佬的项目的GitHub地址

②git remote add upstream A仓库地址

# 查看当前仓库的远程状态
git remote -v
# 被本地仓库添加一个将被同步给 fork 远程的上游仓库
git remote add upstream https://github.com/simplezhli/flutter_deer.git【A仓库地址】
# 这里后面的那个地址就是你fork的项目的源地址
下面会多出两个upstream,说明第二步已经成功了。这就相当于有一个管道在源项目和你fork的项目之间建立了,下面看看如何通信更新。

③同步fork(拉取远程fetch,然后合并到本地)

# 从upstream拉取信息
git fetch upstream [分支名]
# 切换到本地主分支
git checkout master
# 把 upstream/master 分支合并到本地 master 上,这样就完成了同步,并且不会丢掉本地修改的内容。
git merge upstream/master
# 这样,就把源项目同步更新到你的本地仓库中了。 如果再想更新到远程仓库fork(B),只需要:
git push origin master

实操效果图:

3 必备命令

3.1 git remote相关

git remote add [alias] [url]:添加远程仓库
git remote rm [alias]:删除远程仓库

3.2 git fetch相关【拉取最新代码不合并】

git fetch test:从远程仓库test中fetch【有多个远程仓库则指定名字】

如果本地git添加了多个远程仓库,则指定远程仓库名字

3.3 git pull【拉取最新代码并合并】

git pull = git fetch + git merge

3.4 git push

git push [alias] [branch] :推送到指定仓库的指定分支

参考:https://www.jianshu.com/p/021bb953ee8d

经验分享 程序员 微信小程序 职场和发展