git命令使用

git命令使用

注:git commands - use sourcetree

git功能理解与git命令使用 - 经典网站

官网:https://git-scm.com/,可以下载各国语言版本教程“the Pro Git book”
其它:https://www.liaoxuefeng.com/,Git教程

创建分支

    git branch <newbranch>

修改分支名称

    git branch -m <oldbranchname> <newbranchname>

删除分支

    git branch -d <branchname>

切换分支

    git checkout <branch>

创建并切换分支

    git checkout -b <newbranch>

查看分支

    git branch -a # 所有 # all
    git branch -l # 本地 # local
    git branch -r # 远程 # remote

给远程分支创建本地分支

    git pull <repository> <branch>
    git checkout -b <branch> <remote>/<branch>
    案例
        #git checkout -b dev origin/dev
        #fatal: 'origin/dev' is not a commit and a branch 'dev' cannot be created from it
        git pull origin dev
        git checkout -b dev origin/dev

查看尚未暂存时文件更改

    git diff

查看已暂存文件与上次提交版本之间差异

    git diff --cached

查看指定版本与当前版本之间差异

   git diff <commit>

查看任意两个版本之间差异

   git diff <commit> <commit>

查看工作树状态

git status
相当于
Sourcetree - 文件状态(提交)
Sourcetree - File Status (Commit)

查看提交日志

git log
git log --oneline
git log --pretty=oneline
相当于
Sourcetree - History
Sourcetree - 历史

覆盖上一次提交(修改上一次提交)

git commit [--amend] [-m <msg>] [--author=<author>]

<msg>'msg'/"msg"
<author>'name <email>'/"name <email>"
<email>:xxx@xxx.xxx

################################################################################
# 实操案例 - 修改最后一次提交信息(已推送到远程):
# 
# 提交结果:
# commit aba83f522e03e260189a1f75e359f07c56e97058 (HEAD -> dev, origin/dev)
# Author: dy123456 <1234567890@qq.com>
# Date:   Tue Dec 28 03:30:12 2021 -0800
# 
#     修复某某BUG
# 
# 修改提交信息 - Author:
#     git config user.name san.zhang
#     git config user.email san.zhang@dynamic.com
#     git commit --amend -m '再次修复某某BUG' --author='san.zhang <san.zhang@dynamic.com>'
#     git push -f origin dev:dev
# 
# 修改结果:
# commit 24e9409fec570d608f025be4493240f3444df62d (HEAD -> dev, origin/dev)
# Author: san.zhang <san.zhang@dynamic.com>
# Date:   Tue Dec 28 03:52:42 2021 -0800
# 
#     再次修复某某BUG
# 
################################################################################

Sourcetree - 文件状态(提交) - 提交选项 - 修改最后一次修改(勾选) - 提交
Sourcetree - File Status (Commit) - Commit options - Amend latest commit (check) - Commit

修改历史提交信息

git rebase -i HEAD~n # 可修改版本HEAD~n之后所有版本历史提交信息
相当于
Sourcetree - History - 鼠标右键单击某个版本 - Rebase children of <某个版本> interactively
Sourcetree - 历史 - 鼠标右键单击某个版本 - 交互式变基 <某个版本> 的子提交

git rebase -i --root # 可修改所有版本历史提交信息

交互式变基r/reword命令:只在vim或nano中操作
交互式变基e/edit命令:
    需要先在vim或nano中操作,然后交叉使用“git rebase --amend”和“git rebase --continue”

对已经提交到远程仓库的版本的历史提交信息进行修改之后,需要进行强制推送,即,“git push -f origin <branch>”。

对于当前最新提交版本,只需要使用上述“覆盖上一次提交(修改上一次提交)”就行,即,“git commit [--amend] [-m <msg>]”。

修改历史提交信息-主分支强制推送失败

原因:主分支受保护
解决:在远程仓库取消保护 -> 强制推送 -> 在远程仓库开户保护

查看用户名和邮箱

git config user.name
git config user.email

修改用户名和邮箱

git config [--global] user.name "用户名"
git config [--global] user.email "邮箱"
git config --global user.name "用户名"
git config --global user.email "邮箱"
git config user.name "用户名"
git config user.email "邮箱"

使用ssh-keygen命令生成“SSH KEY”

ssh-keygen [-t type] [-C comment] # man ssh-keygen

ssh-keygen -t rsa -C "xxx@xxx.xxx" # xxx@xxx.xxx,即,邮箱。-C选项只是用来指定注释,一般使用邮箱作为注释,不指定注释就会生成默认注释,默认注释格式:"用户名@主机名"。

丢弃本地修改

$ git status
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory) # 1
$ git add -A
$ git status
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage) # 2
Git Manual
git-checkout - Switch branches or restore working tree files # 1
git-reset - Reset current HEAD to the specified state # 2
git reset --hard <commit> # 3
# 示例 # 1
git checkout -- a.c b.c
git checkout -- *
git checkout -- *.c
# 示例 # 2
git reset HEAD a.c b.c
git reset HEAD  *
git reset HEAD *.c
# 示例 # 3
git reset --hard HEAD^
git reset --hard HEAD^^
git reset --hard HEAD^^^
...
git reset --hard HEAD~n

git reset --hard <commit>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值