Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git简易教程 #6

Closed
kiinlam opened this issue Sep 12, 2018 · 0 comments
Closed

Git简易教程 #6

kiinlam opened this issue Sep 12, 2018 · 0 comments
Labels

Comments

@kiinlam
Copy link
Owner

kiinlam commented Sep 12, 2018

Git简易教程

提交

用git初始化目录,该目录称为工作目录

git init

查看工作目录状态

git status

添加文件到工作目录暂存区,小数点表示所有文件

git add <.|file|directory>

提交暂存区里的文件到本地仓库

git commit -m "<commmit message>"

文件比较

比较工作目录与最后一次提交(HEAD)

git diff

比较工作目录与指定提交

git diff <commit-hash>

比较指定文件

git diff <file-name>

比较最近两次commit

git diff HEAD~2 HEAD

指定文件比较的外部编辑器

git difftool

比较暂存区与最后一次提交

git diff --staged

日志

查看commit历史记录

git log
git log --oneline --decorate //查看各个分支当前所指向的对象
git log --grep="<string>" //筛选包含指定串的commit
git log -p -n 2 //查看完整详情,-n指定数量
git log -g master //查看带有引用记录的日志
git log --graph --pretty=oneline --abbrev-commit //查看分支合并图

自定义log格式

git log --pretty=format:"%h %an %ar - %s"

通过帮助文档查看更多log详情

git log --help

查看包含比较信息的commit详情,默认为 HEAD commit

git show
git show <commit-hash> //查看指定commit详情
git show HEAD@{5} //查看5次前的commit记录
git show master@{yesterday} //查看master分支昨天的提交
git show HEAD@{2.months.ago} //查看2个月的记录

查看引用日志,引用日志只在本地仓库存在

git reflog

远程操作

关联当前目录到远程仓库,通常使用 origin 作为别名

git remote add <ref-name> <remote-url>

克隆远程仓库到本地目录,别名自动设置为 origin,目录名与远程仓库的目录名一致

git clone <remote-url>

克隆远程仓库并指定本地目录名

git clone <url> <foldname>

将本地所有commit推送到远程仓库,默认分支名为 master

git push <ref-name> <branch-name>

拉取远程库并自动合并

git pull <ref-name> <branch-name>

拉取远程库但不合并,拉取的文件将下载到独立分支,命名格式为

remotes/<remote-name>/<remote-branch-name>

git fetch <ref-name>

推送分支上的commit

git push <remote_name> <branch_name>

变更提交

取消文件暂存,不改变工作目录

git reset HEAD <.|files|directories>

撤销最后一次commit,返回前一次commit的状态,不改变工作目录

git reset HEAD^

将最后一次HEAD commit指向给定提交,不改变暂存与工作目录

git reset --soft HEAD^

还原到前一次commit,会改变工作目录

git reset --hard HEAD^

撤销工作目录的修改,还原到最后一次commit

git reset --hard HEAD <.|files|directories>

撤销指定提交,并创建新的提交来应用变更

git revert HEAD...HEAD~2

合并

将fetch下来的分支合并

git merge remotes/origin/master

用外部工具进行合并

git mergetool

直接使用本地文件解决冲突

git checkout --ours <file-name>

直接使用远程文件解决冲突

git checkout --theirs <file-name>

使用变基来移植分支,branch上的提交被依次应用到master上

git rebase master branch
git checkout master
git merge branch

合并指定commit

git cherry-pick <hash-id|ref>
git cherry-pick --abort //放弃合并
git cherry-pick --continue //出现冲突时需要走冲突解决流程,再继续

分支

创建分支

git branch dev
git branch <branch-name> <starting-commit> //基于指定分支的最后一次提交创建分支

切换分支

git checkout <branch-name>
git checkout -m dev //检出分支同时合并本地修改
git checkout -b dev //新建并切换分支
git checkout -b dev origin/dev //从远程检出分支

列出分支

git branch
git branch -r //列出远程分支
git branch -av //列出分支,包含远程分支和commit信息
git show-branch //分支详情
git show-branch -a
git show-branch -r

删除分支

git branch -d <branch-name>
git branch -D <branch-name> // 强制删除分支

删除文件

git rm test.txt
git rm --cached test.txt //仅从暂存区删除

修改commit记录

修改最后一次commit消息

git commit --amend

进入rebase交互模式,使用编辑器修改commit记录

git rebase --interactive --root

工作区状态

git stash //保存当前工作区状态
git stash list //查看已保存的工作区状态
git stash apply stash@{0} //恢复指定工作区状态
git stash drop stash@{0} //删除指定工作区状态
git stash pop //恢复并删除工作区

git常用命令

设置用户

git config --global user.name "Your Name"
git config --global user.email "email@example.com"

推送到远程仓库

# 加上-u参数,将远程仓库分支master关联起来
git push -u origin master
# 再次推送
git push origin master

查看远程库的信息

git remote
git remote -v
git remote show
git remote show <remote-name>

推送分支

git push <remote-name> <branch-name>
git push origin dev
git push

重关联本地仓库与远程仓库

git branch --set-upstream dev origin/dev

重定位远程仓库地址

git remote set-url origin <NEW_URL>

标签

git tag tagname //添加标签
git tag //查看标签
git tag v0.9 <hash-id> //在指定commit id上打标签
git show tagname //查看标签信息
git tag -a v0.1 -m "version 0.1 released" <hash-id> //-a 标签名, -m 标签说明
git tag -d tagname //删除标签
git push origin tagname //推送指定标签到远程仓库
git push origin --tags //推送全部标签到远程仓库
git push origin :refs/tags/tagname //删除远程仓库上本地已删除的标签

设置git显示颜色

git config --global color.ui true

设置命令别名

git config --global alias.st status
git config --global alias.ci commit
# 使用字符串方式
# git unstage test.py --> git reset HEAD test.py
git config --global alias.unstage 'reset HEAD'
# 显示最后一次提交信息
git config --global alias.last 'log -1'
# 格式化最后一次的提交信息
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"

配置文件

# 配置Git的时候,加上--global是针对当前用户起作用的,如果不加,那只针对当前的仓库起作用。
# 每个仓库的Git配置文件都放在.git/config文件中
# 当前用户的Git配置文件放在用户主目录下的一个隐藏文件.gitconfig中

Git相关资料

Pro Git

@kiinlam kiinlam added GitHub information of using GitHub Git and removed GitHub information of using GitHub labels Sep 12, 2018
@kiinlam kiinlam changed the title git的基础用法 Git简易教程 Oct 16, 2018
Repository owner locked and limited conversation to collaborators Feb 3, 2023
@kiinlam kiinlam converted this issue into discussion #30 Feb 3, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

1 participant