侧边栏壁纸
  • 累计撰写 251 篇文章
  • 累计创建 138 个标签
  • 累计收到 16 条评论

目 录CONTENT

文章目录

git 杂记

Sherlock
2019-08-09 / 0 评论 / 0 点赞 / 1663 阅读 / 3014 字 / 编辑
温馨提示:
本文最后更新于 2023-10-09,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

管理分支

# 1.查看分支
git branch

# 2.查看远程分支
git branch -r

# 3.查看所有分支
git branch -a

# 4.本地创建新的分支
git branch [branch name]

# 5.切换到新的分支
git checkout [branch name]

# 6.创建+切换分支
git checkout -b [branch name]

# 7.根据已有远程分支创建新的分支
git checkout -b [branch name] origin/oldbranchname
## <==> 等价于
git branch [branch name]
git checkout [branch name]

# 8.将新分支推送到远程仓库
git push origin [branch name]

# 9.删除本地分支
git branch -d [branch name]

# 10.删除远程分支(分支名前的冒号代表删除)
git push origin :[branch name]

# 11.git clone 远程分支
git clone -b dev 代码仓库地址
## dev是分支名称

Git 多分支管理模型可参考:Git分支模型(Git-branching-model)

推送到多个git仓库

git remote add origin git@gitee.com:xxx/yyy.git
git remote add github git@github.com:xxx/yyy.git
git push github master

使用git remote命令可看到多个仓库。

需要注意,此时推送代码时需要对两个仓库分别执行一次push命令

git push origin
git push github

让 git 记住账号密码

git config --global credential.helper store

执行命令后,会在.gitconfig(windows下在 C:\Users\Administrator )中追加

[credential]
	helper = store

Git强制拉取覆盖本地 Pull force

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

强制撤销某一次 push

在刚刚push完,发现不想push该次commit的时候:

git reset --hard <commit_id>
git push origin HEAD --force

查看某一行代码的修改历史

可以查看某行代码由谁写的,在哪个commit中提交的:

git blame {fileName}

其显示格式为:
commit ID | 代码提交作者 | 提交时间 | 代码位于文件中的行数 | 实际代码

这样,我们就可以知道 commit ID 了,然后使用命令:git show {commitID}查看。

丢弃本地修改的所有文件(包括新增、删除的、修改的)

git checkout . && git clean -xdf
# -f 删除(指定路径下)没有被track过文件
# -df 删除(指定路径下)没有被track过文件和目录
# -xdf 删除(指定路径下)没有track过的文件和目录. 不管他是否是.gitignore文件里面指定的文件夹和文件

运行后, 工作目录和缓存区回到最近一次 commit 时候一摸一样的状态,git status会告诉你这是一个干净的工作目录, 又是一个新的开始了!

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区