【笔记】使用git管理代码


笔记

1. 配置git:

2. 下载已有的git仓库

注意:以下指令中的gitlab.GitServer.com需要换成你自己的gitlab服务器域名。

3. 分支相关操作

a. 列出分支(branch)

b. 删除local branch

git branch -d branch-name
或者使用git branch -D branch-name强制删除

c. 删除remote branch

git push origin --delete remote-branch-name

d. push到远程

git push origin remote-branch-name 注:origin是给远程设置的名字,可以有其他名字

e. 创建分支

f. 从远程下branch

git checkout -b local-branch-name remote/branch-name

4. commit相关操作

git commit提交的时候有很多可选的参数配置,下面列举一些常用的用法:

5. tracked repositories

git remote -v

git remote add upstream git_url

git push --set-upstream "origin(remote)" remote-branch-name

git reset --soft origin/master

https://help.github.com/articles/fork-a-repo/#platform-linux

https://help.github.com/articles/syncing-a-fork/

6. 已经push到远程的最近一个commit强制删除

【危险操作,不建议做!!!】
先reset到前一个commit:git reset --hard HEAD~1
然后强制push:git push --force [origin remote-branch-name]

https://www.cnblogs.com/code1992/p/8974896.html

7. merge部分文件的修改到master分支

https://segmentfault.com/a/1190000008360855

8. 修改历史提交中的作者名字或邮箱

【危险操作,对于多人合作的项目不要这么做!!!】

https://www.git-tower.com/learn/git/faq/change-author-name-email

参考资料

https://git-scm.com/book/en/v2

后记

本文最早发表在新浪博客【原文链接】。当时我把新浪博客当作个人笔记使用,2022年4月新浪博客已关闭访问,标志着一个时代的结束。