Git命令手册

git branch 分支

  • 列出所有分支(本地)
    git branch

  • 列出所有分支(包括远程)
    git branch -a

  • 删除分支
    git branch -d [branch]

git checkout 分支

  • 切换到分支
    git checkout develop

  • 创建并切换到分支
    git checkout -b debug

    • 基于特定 commit 创建新分支new并切换到new分支
      git checkout -b new <commit>
  • clone 远程仓库分支并切花到分支
    git checkout -b develop origin/develop

git checkout – 撤销回退修改

  • 撤销工作区的修改(恢复到暂存区状态或历史区,也就是把红色的修改撤销,但不能删除新增加的文件)
    git checkout – .

git cherry-pick 合并

  • 合并单个commit到当前分支
    git cherry-pick <commit>

git clean

  • 强制移除工作区所有新增加的文件夹和文件
    git clean -df

git config 配置

  • 查看配置信息
    git config --list

  • 解决gitk中文乱码问题
    git config --global gui.encoding “utf-8”

  • 修改提交编码
    git config --global i18n.commitEncoding “{utf-8|gbk}”

git merge 合并

  • 合并指定分支到当前分支
    git merge develop

    • 不直接commit
      git merge --no-commit develop
  • 取消merge
    git merge --abort

  • 不显示log
    git merge – squash

  • 基于特定 commit 合并 develop 分支到 master 分支

    1. 切换到develop分支
      git checkout develop
    2. 基于特定 commit 创建一个新分支new
      git checkout -b new <commit>
    3. 切换到master分支
      git checkout master
    4. 合并new分支到master分支
      git merge new
    5. 删除new分支
      git branch -d new
  • 解决冲突
    as -> VCS -> Git -> Resolve Conflicts…

git rebase 合并

  • 合并指定分支到当前分支(合并时间线,)
    git rebase develop

  • 合并develop分支几个连续commit到master

    1. 切换到develop分支
      git checkout develop
    2. 基于最后一个需要合并的 commit 创建一个新分支new
      git checkout -b new <commit>
    3. 从第一个需要合并的commit开始rebase这个新分支的commit到master
      git rebase --onto master <commit>

git reflog 查看操作

  • 查看所有的提交和分支切换信息
    git reflog

git tag 打标签

  • 显示所有的tag
    git tag
  • 查看某个版本系统的tag
    git tag -l ’ v1.0.*’
  • 创建标签
    第一种: git tag -a v1.0.0 -m ‘内容’
    第二种: git tag v1.0.0
  • 查看标签的详情
    git show v1.0.0
  • 推送标签
    git push origin v1.0.0
  • 删除本地标签
    git tag -d v1.0.0
  • 删除远程标签
    git push origin :refs/tags/v1.0.0

git reset 撤销回退

  • 回退到某个commit

    • –mixed 修改版本库,修改暂存区,保留工作区(默认是这个)
      git reset --mixed <commit>
    • –soft 修改版本库,保留暂存区,保留工作区
      git reset --soft <commit>
    • –hard 修改版本库,修改暂存区,修改工作区
      git reset --hard <commit>
  • 暂存区到工作区
    git reset HEAD . (类似get reset --mixed HEAD)

  • 撤销最近一次Commit
    git reset HEAD^

git 回撤第一条提交记录

  • git 回撤第一条提交记录

git stash 保存修改

  • 保存工作区或者暂存区的修改以便切换分支
    git stash

  • 查看所有的保存
    git stash list

  • 恢复工作区或者暂存区的修改
    git stash apply

远程同步

  • 添加远程仓库
    git remote add origin xxx.git

  • 取回远程仓库的变化,并与本地分支合并
    git pull [remote] [branch]

  • 将远程主机的某个分支的更新取回,并与本地指定的分支合并,完整格式可表示为:
    git pull <远程主机名> <远程分支名>:<本地分支名>

  • 本地回退Commit后强制推送到远程
    git push -f

  • 推送到远程
    git push -u origin master

Personal Access Token

  • 新仓库在clone的时候就输入账号和Person Access Token即可
$ git clone https://github.com/username/repo.git
Username: your_username // 你的用户名
Password: your_token // 你的Personal Access Token
  • 已有仓库使用Personal Access Token
git config --system --unset credential.helper //不保存密码
git config --global credential.helper store //保存密码

对比

  • 分支对比
    git log --left-right develop…release 2边不一样的commit,左箭头 < 表示是 dev 的,右箭头 > 表示是 release的
    git log develop ^release 查看 dev中log有的commit,而 release中log没有的commit
    git diff develop origin/develop --stat 显示出所有有差异的文件(不详细,没有对比内容)
  • 对比工具对比
    git difftool

别名

  • 显示 Log 信息
    git config --global alias.lg “log --color --graph --pretty=format:‘%Cred%h%Creset%C(yellow)%d%Creset%s%Cgreen(%cd)%C(bold blue)<%an>%Creset’ --abbrev-commit --date=format:‘%Y-%m-%d’”
    git config --global alias.lg “log --graph --decorate --oneline --pretty=format:‘%Cred%h - %C(bold white) %s %Creset %C(yellow)%d %C(cyan) <%cd> %Creset %Cgreen(%cn)’ --abbrev-commit --date=format:‘%Y-%m-%d’”
    git config --global alias.lg “log --color --graph --pretty=format:‘%Cred%h%Creset%C(yellow)%d%Cgreen(%cd)%Creset%s%C(bold blue)<%an>%Creset’ --abbrev-commit --date=format:‘%Y-%m-%d’”

  • 显示所有分支 Log 信息
    git config --global alias.la “log --all --color --graph --pretty=format:‘%Cred%h%Creset%C(yellow)%d%Creset%s%Cgreen(%cd)%C(bold blue)<%an>%Creset’ --abbrev-commit --date=format:‘%Y-%m-%d’”

工具

  • git查看有哪些对比工具可以设置命令
    git difftool --tool-help

  • 设置对比工具
    git config --global diff.tool bc3
    git config --global difftool.bc3.path “C:\Program Files (x86)\Beyond Compare 3\BCompare.exe”

  • 设置合并工具
    git config --global merge.tool bc3
    git config --global mergetool.bc3.path “C:\Program Files (x86)\Beyond Compare 3/BCompare.exe”

您的姓名和邮件地址基于登录名和主机名进行了自动设置。请检查它们正确
与否。您可以对其进行设置以免再出现本提示信息:

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

设置完毕后,您可以用下面的命令来修正本次提交所使用的用户身份:

git commit --amend --reset-author
好的,以下是 Git 命令的详细手册: ## Git 基础命令 ### 初始化一个 Git 仓库 ```sh git init ``` 使用该命令可以在当前目录下初始化一个 Git 仓库。 ### 添加文件到 Git 仓库 ```sh git add <file> ``` 使用该命令可以将文件添加到 Git 仓库中,`<file>` 参数为文件名。 ### 提交文件到 Git 仓库 ```sh git commit -m "commit message" ``` 使用该命令可以将添加到 Git 仓库中的文件提交到仓库中,`-m` 参数表示提交信息,需要在引号中输入提交信息。 ### 查看 Git 仓库状态 ```sh git status ``` 使用该命令可以查看 Git 仓库中文件的状态,如未跟踪、已修改等。 ### 查看 Git 仓库提交历史 ```sh git log ``` 使用该命令可以查看 Git 仓库的提交历史。 ### 撤销 Git 仓库中的修改 ```sh git checkout -- <file> ``` 使用该命令可以撤销 Git 仓库中未提交的修改,`<file>` 参数为文件名。 ### 撤销 Git 仓库中的提交 ```sh git reset HEAD^ ``` 使用该命令可以撤销 Git 仓库中的最近一次提交。 ### 回退到 Git 仓库中的某一版本 ```sh git reset <commit> ``` 使用该命令可以回退到 Git 仓库中的某一版本,`<commit>` 参数为提交记录的哈希值。 ## Git 分支命令 ### 创建一个新分支 ```sh git branch <branch-name> ``` 使用该命令可以创建一个新的 Git 分支,`<branch-name>` 参数为分支名称。 ### 切换到指定分支 ```sh git checkout <branch-name> ``` 使用该命令可以切换到指定的 Git 分支,`<branch-name>` 参数为分支名称。 ### 查看所有分支 ```sh git branch ``` 使用该命令可以查看 Git 仓库中所有的分支。 ### 合并指定分支到当前分支 ```sh git merge <branch-name> ``` 使用该命令可以将指定的 Git 分支合并到当前分支中,`<branch-name>` 参数为分支名称。 ### 删除分支 ```sh git branch -d <branch-name> ``` 使用该命令可以删除指定的 Git 分支,`<branch-name>` 参数为分支名称。 ## Git 远程仓库命令 ### 关联一个远程仓库 ```sh git remote add origin <remote-url> ``` 使用该命令可以将本地的 Git 仓库与指定的远程仓库关联,`<remote-url>` 参数为远程仓库的 URL。 ### 推送本地仓库到远程仓库 ```sh git push -u origin master ``` 使用该命令可以将本地的 Git 仓库推送到指定的远程仓库,`-u` 参数表示设置默认的远程仓库,`origin` 参数表示远程仓库的名称,`master` 参数表示要推送的分支名称。 ### 从远程仓库拉取代码 ```sh git pull origin master ``` 使用该命令可以从指定的远程仓库拉取代码,`origin` 参数表示远程仓库的名称,`master` 参数表示要拉取的分支名称。 ### 克隆远程仓库 ```sh git clone <remote-url> ``` 使用该命令可以克隆指定的远程仓库到本地。 ### 查看远程仓库信息 ```sh git remote -v ``` 使用该命令可以查看 Git 仓库中所有的远程仓库信息。 ## Git 高级命令 ### 查看 Git 仓库中的文件差异 ```sh git diff <commit1> <commit2> <file> ``` 使用该命令可以查看 Git 仓库中两个提交之间指定文件的差异,`<commit1>` 参数为较早的提交,`<commit2>` 参数为较晚的提交,`<file>` 参数为指定的文件名。 ### 通过 SSH 协议访问远程仓库 ```sh git remote set-url origin git@github.com:<username>/<repo-name>.git ``` 使用该命令可以通过 SSH 协议访问远程仓库,`<username>` 参数为 GitHub 用户名,`<repo-name>` 参数为仓库名称。 ### 配置 Git 仓库 ```sh git config --global user.name "<username>" git config --global user.email "<email>" ``` 使用该命令可以配置 Git 仓库的用户信息,`<username>` 参数为用户名,`<email>` 参数为用户邮箱。 ## 结语 以上就是 Git 命令的详细手册,希望对你有所帮助。记住,熟练掌握 Git 命令是成为一名优秀程序员的必要条件之一。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值