git常用命令

 


一、git的框架结构

image

二、git 常用命令

#!/bin/bash
git branch -a 查看所有分支
git status  查看git状态
git check release 
git check -b release-fix 从当前分支切出行的release-fix分支
git push orgin release
git fetch 
git pull origin release
git stash 
git stash pop
git stash drop 
git merge release    将release合并到当前分支
git merge --abort
git restore .    撤销工作区当前目录的所有   危险命令 除非知道是不要的修改
git restore  thisfile   只撤销这个文件
git clone 
git init 
git add  . 
git commit  -m  " "  
git branch update origin   更新远程仓库列表
git reset --hard HARD~
git log

//稍微不常用也需要熟悉  vscode代替

git restore --staged .     撤销缓存区的所有文件到工作区
git reset HEAD  .   作用同上    不建议使用
git diff     不常用 ,vscode代替

我想合并某一个提交 还不知道如何操作

合并冲突  忘了变基是啥作用了  git rebase

git 的进阶 对于git book阅读的提升 git book在看3.1分支简介

#!/bin/bash
  git log --grep=     仅显示提交说明中包含指定字符串的提交
  git commit -m "xxx" --amend    //--amend   替换上一次的提交信息 或先添加某些修改再重新上一次提交
  git restore    Git version 2.23.0 introduced

1、忽略文件

一般我们总会有些文件无需纳入 Git 的管理,也不希望它们总出现在未跟踪文件列表。 通常都是些自动生成的文件,比如日志文件,或者编译过程中创建的临时文件等。 在这种情况下,我们可以创建一个名为 .gitignore 的文件,列出要忽略的文件的模式。 来看一个实际的 .gitignore 例子:

#!/bin/bash
$ cat .gitignore
*.[oa]
*~

第一行告诉 Git 忽略所有以 .o 或 .a 结尾的文件。一般这类对象文件和存档文件都是编译过程中出现的。 第二行告诉 Git 忽略所有名字以波浪符(~)结尾的文件,许多文本编辑软件(比如 Emacs)都用这样的文件名保存副本。 此外,你可能还需要忽略 log,tmp 或者 pid 目录,以及自动生成的文档等等。 要养成一开始就为你的新仓库设置好 .gitignore 文件的习惯,以免将来误提交这类无用的文件。

文件 .gitignore 的格式规范如下:

  • 所有空行或者以 # 开头的行都会被 Git 忽略。

  • 可以使用标准的 glob 模式匹配,它会递归地应用在整个工作区中。

  • 匹配模式可以以(/)开头防止递归。

  • 匹配模式可以以(/)结尾指定目录。

要忽略指定模式以外的文件或目录,可以在模式前加上叹号(!)取反。

所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。 星号(*)匹配零个或多个任意字符;[abc] 匹配任何一个列在方括号中的字符 (这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c); 问号(?)只匹配一个任意字符;如果在方括号中使用短划线分隔两个字符, 表示所有在这两个字符范围内的都可以匹配(比如 [0-9] 表示匹配所有 0 到 9 的数字)。 使用两个星号(** )表示匹配任意中间目录,比如 a/**/z 可以匹配 a/z 、 a/b/z 或 a/b/c/z 等。

我们再看一个 .gitignore 文件的例子:

# 忽略所有的 .a 文件
*.a

# 但跟踪所有的 lib.a,即便你在前面忽略了 .a 文件
!lib.a

# 只忽略当前目录下的 TODO 文件,而不忽略 subdir/TODO
/TODO

# 忽略任何目录下名为 build 的文件夹
build/

# 忽略 doc/notes.txt,但不忽略 doc/server/arch.txt
doc/*.txt

# 忽略 doc/ 目录及其所有子目录下的 .pdf 文件
doc/**/*.pdf

Tip:

  • GitHub 有一个十分详细的针对数十种项目及语言的 .gitignore 文件列表, 你可以在 https://github.com/github/gitignore 找到它。
  • 在最简单的情况下,一个仓库可能只根目录下有一个 .gitignore 文件,它递归地应用到整个仓库中。 然而,子目录下也可以有额外的 .gitignore 文件。子目录中的 .gitignore 文件中的规则只作用于它所在的目录中。

2、远程仓库的使用

克隆一个远程服务器的仓库,就会得到一个简写 默认origin

git remote
origin
如果远程仓库不止一个,比如在git上有,在github上也有,都需要去维护的话

#!/bin/bash
git remote -v
bakkdoor  https://github.com/bakkdoor/grit (fetch)
bakkdoor  https://github.com/bakkdoor/grit (push)
cho45     https://github.com/cho45/grit (fetch)
cho45     https://github.com/cho45/grit (push)
defunkt   https://github.com/defunkt/grit (fetch)
defunkt   https://github.com/defunkt/grit (push)
koke      git://github.com/koke/grit.git (fetch)
koke      git://github.com/koke/grit.git (push)
origin    git@github.com:mojombo/grit.git (fetch)
origin    git@github.com:mojombo/grit.git (push)

就有了如下操作

#!/bin/bash
git remote [-v | --verbose]
   或:git remote add [-t <分支>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <名称> <地址>
   或:git remote rename <旧名称> <新名称>
   或:git remote remove <名称>
   或:git remote set-head <名称> (-a | --auto | -d | --delete | <分支>)
   或:git remote [-v | --verbose] show [-n] <名称>
   或:git remote prune [-n | --dry-run] <名称>
   或:git remote [-v | --verbose] update [-p | --prune] [(<组> | <远程>)...]
   或:git remote set-branches [--add] <名称> <分支>...
   或:git remote get-url [--push] [--all] <名称>
   或:git remote set-url [--push] <名称> <新的地址> [<旧的地址>]
   或:git remote set-url --add <名称> <新的地址>
   或:git remote set-url --delete <名称> <地址>

3、打tag

#!/bin/bash
git tag     列出标签
git tag -a v1.4 -m "my version 1.4"     附属标签   需要-a   -m
git show v1.4     查看v.14标签信息和提交
git tag v1.4-lw         轻量标签 git show,不会看到额外的标签信息。 命令只会显示出提交信息
git push origin v1.5       别忘了把标签(v1.5)推到远程仓库    
git push origin --tags        把全部标签一起推到远程仓库
git push origin --delete v1.5  删除远程仓库的v1.5标签
git checkout -b newbranch v1.5   用标签(v1.5)切出分支修改

4、 后期打标签

假设提交历史是这样的:

#!/bin/bash
git log --pretty=oneline

15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch 'experiment'
9fceb02d0ae598e95dc970b74767f19372d61af8 updated rakefile
964f16d36dfccde844893cac5b347e7b3d44abbc commit the todo
8a5cbc430f1a9c3d00faaeffd07798508422908a updated readme

假设在 v1.2 时忘记给项目打标签,可以在之后补上标签。 要在那个提交上打标签,需要在命令的末尾指定提交的校验和(或部分校验和):

git tag -a v1.2 9fceb02

5、git 命令起别名

#!/bin/bash
git config --global alias.co checkout  co是checkout的别名 
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.ps push
git config --global alias.pl pull
#!/bin/bash
git config --global alias.last 'log -1 HEAD'
这样,可以轻松地看到最后一次提交:

git last
commit 66938dae3329c7aebe598c2246a8e6af90d04646
Author: Josh Goebel <dreamer3@example.com>
Date:   Tue Aug 26 19:48:51 2008 +0800

    test for current head

    Signed-off-by: Scott Chacon <schacon@example.com>

posted on   OPBrother  阅读(44)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示