7. Git 命令实战

初始化仓库并推送到远端

  1. 初始化
echo "# MyProject" >> README.md
git init 
  1. 然后可以愉快的进行开发了。。。
add .
commit 'lalala'
  1. 进行推送(如果本身已经是 git 仓库了,直接走到第 3 步骤)
# 将本地仓库关联一个远程库
git remote add origin git@github.com:someone/MyProject.git
# 加上了 -u 参数,Git 不但会把本地的master分支内容推送的远程新的master分支,还会把本地的 master 分支和远程的 master 分支关联起来,在以后的推送或者拉取时就可以简化命令
git push -u origin master

Git 代码库迁移

# 从原地址克隆一份裸版本库
git clone --bare https://git.example.com/your/project.git your_path
cd your_path
git remote set-url origin https://codeup.aliyun.com/5eacd74338076f00011bc59e/hexo-src.git
git push --mirror

或者不 set-url origin, 而是最后两条命令变成一条命令, 进行推送

git push --mirror https://codeup.aliyun.com/5eacd74338076f00011bc59e/hexo-src.git
  • 其中 git clone --bare 创建的克隆版本库都不包含工作区,直接就是版本库的内容,这样的版本库称为裸版本库。

  • 其中 git clone --mirror 远程跟踪设置,所以如果你运行 git 远程更新所有参考将被覆盖从原点,如果你刚刚删除镜像并重新登记。正如文档最初说的,它是一面镜子。它应该是一个功能相同的副本,可以与原件互换。

  • git push --mirror
    Instead of naming each ref to push, specifies that all refs under
    refs/ (which includes but is not limited to refs/heads/,
    refs/remotes/, and refs/tags/) be mirrored to the remote
    repository. Newly created local refs will be pushed to the remote
    end, locally updated refs will be force updated on the remote end,
    and deleted refs will be removed from the remote end. This is the
    default if the configuration option remote.<remote>.mirror is set.

简而言之 -- mirror 强制推送 all refs under refs/ 下的所有. 保持绝对的同步.

撤销修改

场景1:当你改乱了工作区某个文件的内容,还未提交到工作区, 且想直接丢弃工作区的修改时,用命令git checkout -- file。用干净暂存区内容(同版本库)去覆盖工作区的内容。

场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了场景1,第二步按场景1操作。

reset 和 revert

本地分支可以 reset, 回退分支
revert 回归分支会生成新的提交记录, 远程分支建议如此操作.

如何修改老旧的commit信息

git rebase -i commit-id
commit-id 指的是要修改的节点的父节点.
使用 r 命令

如何多个 commit 整理合并成

git rebase -i commit-id
commit-id 指的是要修改的节点的父节点.

然后使用 s 标记进行精简。

git 移除已提到到版本库的文件

请使用 git rm --cached 命令

发现 .idea 文件夹下的文件还有变更被提交,这是因为在使用 gitignore 之前,此文件就以及被跟踪了,这样的话需要移除跟踪,如下命令:

移除单个文件
git rm --cached --force ydq-api/ydq-api.iml

移除指定文件夹即文件夹下所有文件:
git rm --cached --force -r .idea/

git 如何修改远程 URL

# 先查看remote的名字
git remote -v 
origin  http://aaa.bbb.ccc.ddd/be/preser-image.git (fetch)
origin  http://aaa.bbb.ccc.ddd/be/preser-image.git (push)
# 使用`git remote set-url` 修改 remote_url地址, 这里假设你的remote是origin
git remote set-url origin http://117.50.94.8/be/preser-image.git

删除远程分支

方法一

git push 主机名  :远程分支

如果一次性删除多个,可以写多个

git push 主机名 :远程分支名 :远程分支名  :远程分支名

方法二
另外一个删除分支的命令是

git push 主机名 --delete 远程分支名

方法三

git branch -r -d origin/branch-name

自动补全

在输入 Git 命令的时候可以敲两次跳格键(Tab),就会看到列出所有匹配的可用命令建议

设置 http 代理

# 开启 http 代理
git config --global http.proxy http://10.5.3.9:80
# 开启 https 代理
git config --global https.proxy https://10.5.3.9:80

# 取消 对应 http 代理
git config --global --unset http.proxy
# 取消 对应 https 代理
git config --global --unset https.proxy

参考

很好的参考文章:
https://fle.github.io/git-tip-keep-your-branch-clean-with-fixup-and-autosquash.html

https://www.jianshu.com/p/53806c704f6a

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.git的安装 1.1 在Windows上安装Git msysgit是Windows版的Git,从https:/...
    落魂灬阅读 12,723评论 4 54
  • git常用命令 GIT常用命令备忘:http://stormzhang.com/git/2014/01/27/gi...
    新篇章阅读 8,660评论 1 26
  • 1. 安装 Github 查看是否安装git: $ git config --global user.name "...
    Albert_Sun阅读 13,737评论 9 163
  • 一 像我这种爱自由的人 甘愿为你放弃 做事三分热度的我 爱你却用了七分 二 自先成熟稳重而后爱人 我想有猫也想有你...
    余辜r阅读 318评论 10 5
  • 昨夜寒潮来 风铺开 “呜呜”地赶散密布的云 云散,云聚,聚了再散 月亮隐现 今晨 梧桐和银杏的叶子仅剩几片 湖溪的...
    风言无语阅读 510评论 22 58