-
部分地方使用了自己的github用户名
-
git config --global user.name "Hexagram" git config --global user.email "[email protected]"
- 登记名字和邮箱以使用git
-
git init 初始化
-
git status
-
git diff
- git diff HEAD -- file 查看file在工作区和版本库里最新版本的区别
-
git add
-
git commit -m
-
git log
-
git reset --hard HEAD^
- git reset HEAD file 把暂存区的修改退回的工作区
-
git reflog 记录了使用的每一次命令
-
git checkout -- file 丢弃工作区的修改
- 按情况回到版本库或者暂存区的状态
- 实质是使用已存文件替换工作区的版本
-
git reset HEAD file
-
git rm file
连接到远程仓库:
- ssh-keygen -t rsa -C "your_email"
- 建立一个本地的ssh公钥和私钥,再把公钥丢到GitHub上
- 建立时没有设置密码
- git remote add origin [email protected]:lmx-Hexagram/ackonwledge_arrangement.git
- [email protected]:用户名/仓库名.git
- git push -u origin master
- 第一次使用时需要用-u参数,使其在推送本地master分支的同时,把远程仓库和本地仓库联系起来,以简化以后的命令
- git push origin master以后只要这样就可以了
- git clone [email protected]:lmx-Hexagram/ackonwledge_arrangement.git
- 从远程仓库克隆
- [email protected]:用户名/仓库名.git
- 也可以使用 git clone https://github.com/lmx-Hexagram/ackonwledge_arrangement.git
- 也就是可以使用SSH和https两种协议
分支:
- git checkout -b dev
- Switched to a new branch 'dev'
- 创建并切换到分支'dev'
- -b 参数表示创建并切换
- 以上语句等价于 git branch dev + git checkout dev
- git branch 列出所有分支
- git checkout branch_name
- git merge dev
- 把dev分支的结果合并到'当前所在'分支上
- 此时是使用
Fast-forward
也就是'快进模式',是直接把master指向dev的当前提交
- git branch -d dev 删除分支
- git branch -D dev 强制删除没有合并过的分支
- git switch -c dev 创建并切换的新分支dev
- git switch branch_name 切换到已有的分支
- 推荐使用
switch
命令来切换和新建分支
- git log --graph 查看分支合并图
- git merge --no--ff -m "say_sth" branch_name
- 使用
--no--ff
参数使在合并时禁用Fast-forward模式,并创建一个commit
- 使用
暂存工作现场:
- git stash 保存现在的工作现场(在当前分支)
- git stash list
- git stash apply 恢复暂存的工作现场
- git stash apply stash@{num} 使用这种方式指定要恢复的工作现场
- git stash drop 删除暂存的工作现场
- git stash pop 恢复并删除
- git cherry-pick name_of_commit 在当前分支重现指定的提交(用来修bug)
远程多人合作:
- git remote -v 查看远程库的信息
- git push origin branch_name 将本地分支推送到origin
- git checkout -b branch_name orgin/branch_name 在本地创建和远程分支对应的分支,名字一致(不要在这里乱皮)
- git branch --set-upstream branch_name origin/branch_name 建立本地和远程分支的关联
标签:
- git tag命令簇
- git tag 查看所有标签
- git tag tag_name commit_name
- for example:
- git tag v1.0 378a2a
- 若没有commit_name则默认给当前分支加标签
- git show tag_name 列出此tag的详细信息
- git tag -a tag_name -m "sth_to_say" commit_name
- 打上标签并给标签加上说明
- git tag -d tag_name 删除标签
- git push origin tag_name 将标签推送到远程
- git push origin --tags 将本地所有的标签推送到远程
- git push origin :refs/tags/tag_name 删除远程标签(目前不太懂原理,最好先删除本地标签)
在github上工作
- git clone [email protected]:lmx-Hexagram/learngit.git
- 先在github上把项目folk到自己的库里,再从github上自己的库里把项目clone下来
- 在自己本地修改好后,pull到github上自己的库里
- 在github的项目中发起pull request
git的自定义:
- git config --global color.ui true 显示颜色
- 忽略特殊文件
- 在工作区根目录创建
.gitignore
文件 - 在https://github.com/github/gitignore中找到要用的配置文件
- git add -f file_name 强制添加被忽略的文件
- git check-ignore -v file_name 查看时那一条忽略规则,忽略了该文件
- 还有
.gitignore要放在版本库里,换言之不能忽略该文件,这样可以方面以后管理
- 在工作区根目录创建
- git config --global alias.the_name_as_your_convinent command_of_git
- 以这种方式定义别名,下次使用时可以直接
git the_name_as_your_convinent
- --global参数使该命令针对这台电脑所有Git仓库有效
- git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" 神仙用法
- 以这种方式定义别名,下次使用时可以直接
以上
lmx-Hexagram