GIT 常用

❀ 初始操作

根目录创建 .gitignore 文件,内容可以为:

.git/
.ssh/
.vs/
bin/
obj/
x64/
x86/

.gitignore 文件的格式规范:
所有空行或者以注释符号 # 开头的行都会被 Git 忽略。
最后跟反斜杠(/)说明要忽略的是目录。
星号(*)匹配零个或多个任意字符。
问号(?)只匹配一个任意字符
[] 匹配任何一个列在方括号中的字符(如[abc]要么匹配一个 a,要么匹配一个 b,要么匹配一个 c)
如果在方括号中使用短划线分隔两个字符,表示所有在这两个字符范围内的都可以匹配(如[0-9]表示匹配所有 0 到 9 的数字)
模式前加上惊叹号(!)取反。

  • 本地初始化
git init
git add .
git config --global user.name "maber"
git config --global user.email "super@made.beer"
git commit -m "first commit"

git remote add origin https://xxxx.com/xxxx.git
git push -u origin master
  • SSH clone
ssh-keygen -t rsa -C "super@made.beer" -f ~/.ssh/id_rsa
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

#windows
pbcopy < ~/.ssh/id_rsa.pub

#linux
sudo apt-get install xclip
xclip -sel clip < ~/.ssh/id_rsa.pub

#mac
pbcopy < ~/.ssh/id_rsa.pub

#测试ssh
ssh -T git@github.com

git clone git@github.com:xxx/xxxx.git


ssh-agent bash -c 'ssh-add /home/me/my_private_key; git clone git@bitbucket.org:uname/test-git-repo.git'

放弃所有修改(未add): git checkout .
放弃文件修改(未add): git checkout -- filepathname
放弃所有修改(已add): git reset HEAD .
放弃文件修改(已add): git reset HEAD filepathname
放弃上次修改(已提交): git reset --hard HEAD^

  • 拉取分支:
git.exe pull -v --progress "origin"
git.exe pull --progress -v --no-rebase "origin"
  • 获取分支:
git.exe fetch -v --progress "origin"
  • 推送master分支到远程console分支:
git.exe push --progress "origin" master:console

git.exe push --all --progress "origin"
  • 切换远程master分支到本地develop分支
git.exe checkout --no-track -b develop remotes/origin/master --
  • 切换本地console分支
git.exe checkout console --
  • 取消对文件的修改。还原到最近的版本,废弃本地做的修改。
git checkout -- <file>
  • 取消已经暂存的文件。即,撤销先前"git add"的操作
git reset HEAD <file>...
  • 修改最后一次提交。用于修改上一次的提交信息,或漏提交文件等情况。
git commit --amend
  • 回退所有内容到上一个版本
git reset HEAD^
  • 回退a.py这个文件的版本到上一个版本
git reset HEAD^ a.py  
  • 向前回退到第3个版本
git reset –soft HEAD~3  
  • 将本地的状态回退到和远程的一样
git reset –hard origin/master  
  • 回退到某个版本
git reset 057d  
  • 回退到上一次提交的状态,按照某一次的commit完全反向的进行一次commit.(代码回滚到上个版本,并提交git)
git revert HEAD
  • 更新获取
git.exe push --all --progress "origin"
git.exe checkout -B master remotes/origin/master --
git.exe pull -v --progress --force "origin"
git.exe fetch -v -progress --force "origin" develop:remotes/origin/develop
git.exe push -v --progress --force "origin" master:develop
git.exe push -v --progress --force "origin" chromely:develop
  • 关联远程分支
git remote add origin git@github.com:git_username/repository_name.git
  • 切换关联的远程分支
git remote remove origin
git branch --set-upstream-to origin/develop develop
//或者
git remote add origin git@github.com:git_username/repository_name.git

❀子模块操作

  • 添加子模块
git submodule add <url> <path>
git submodule add  https://116.247.99.118:10000/zhfayuan/win-sdk-release.git TermComKit/3rd

  • 克隆项目后,默认子模块目录下无任何内容。需要在项目根目录执行如下命令完成子模块的下载
git submodule init
git submodule update
git submodule update --remote --merge

或者:

git submodule update --init --recursive
  • 删除子模块
rm -rf 子模块目录 删除子模块目录及源码
vi .gitmodules 删除项目目录下.gitmodules文件中子模块相关条目
vi .git/config 删除配置项中子模块相关条目
rm .git/module/* 删除模块下的子模块目录,每个子模块对应一个目录,注意只删除对应的子模块目录即可
git rm --cached 子模块名称
git rm -r --cached open_source_code/openh264

❀常见问题处理

fatal: refusing to merge unrelated histories

git pull origin master --allow-unrelated-histories
git push origin master:master

解决 git SSL certificate problem: self signed certificate

git config --global http.sslVerify false

清除密码

git credential-manager uninstall
git config --system --unset credential.helper
git config --global credential.helper wincred
git config --global credential.helper store

GIT设置代理:

git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'
git config --global --unset http.proxy

❀ GIT 配置示例

[core]
	bare = false
	repositoryformatversion = 0
	filemode = false
	symlinks = false
	ignorecase = true
	logallrefupdates = true
	autocrlf = true
	excludesfile =~/.gitignore
[credential]
  helper = cache --timeout=28800
[remote "origin"]
	url = git@github.com:inrg/SuperGenerator.git
	fetch = +refs/heads/*:refs/remotes/origin/*
	pushurl = git@github.com:inrg/SuperGenerator.git
	puttykeyfile = D:\Tools\1st
[branch "console"]
	remote = origin
	merge = refs/heads/console
[branch "winform"]
	remote = origin
	merge = refs/heads/master
[remote]
	pushdefault = origin

❀ GIT 命令

用法:git commit [<选项>] [--] <路径规格>...

    -q, --quiet           提交成功后不显示概述信息
    -v, --verbose         在提交说明模板里显示差异

提交说明选项
    -F, --file <文件>     从文件中读取提交说明
    --author <作者>       提交时覆盖作者
    --date <日期>         提交时覆盖日期
    -m, --message <说明>  提交说明
    -c, --reedit-message <提交>
                          重用并编辑指定提交的提交说明
    -C, --reuse-message <提交>
                          重用指定提交的提交说明
    --fixup <提交>        使用 autosquash 格式的提交说明用以修正指定的提交
    --squash <提交>       使用 autosquash 格式的提交说明用以压缩至指定的提交
    --reset-author        现在将该提交的作者改为我(和 -C/-c/--amend 参数共用)
    -s, --signoff         添加 Signed-off-by: 签名
    -t, --template <文件>
                          使用指定的模板文件
    -e, --edit            强制编辑提交
    --cleanup <模式>      设置如何删除提交说明里的空格和#注释
    --status              在提交说明模板里包含状态信息
    -S, --gpg-sign[=<key-id>]
                          GPG 提交签名

提交内容选项
    -a, --all             提交所有改动的文件
    -i, --include         添加指定的文件到索引区等待提交
    --interactive         交互式添加文件
    -p, --patch           交互式添加变更
    -o, --only            只提交指定的文件
    -n, --no-verify       绕过 pre-commit 和 commit-msg 钩子
    --dry-run             显示将要提交的内容
    --short               以简洁的格式显示状态
    --branch              显示分支信息
    --ahead-behind        计算完整的领先/落后值
    --porcelain           机器可读的输出
    --long                以长格式显示状态(默认)
    -z, --null            条目以 NUL 字符结尾
    --amend               修改先前的提交
    --no-post-rewrite     绕过 post-rewrite 钩子
    -u, --untracked-files[=<模式>]
                          显示未跟踪的文件,“模式”的可选参数:all、normal、no。(默认:all)
用法:git config [<选项>]

配置文件位置
    --global              使用全局配置文件
    --system              使用系统级配置文件
    --local               使用仓库级配置文件
    --worktree            使用工作区级别的配置文件
    -f, --file <文件>     使用指定的配置文件
    --blob <数据对象 ID>  从给定的数据对象读取配置

操作
    --get                 获取值:name [value-regex]
    --get-all             获得所有的值:key [value-regex]
    --get-regexp          根据正则表达式获得值:name-regex [value-regex]
    --get-urlmatch        获得 URL 取值:section[.var] URL
    --replace-all         替换所有匹配的变量:name value [value_regex]
    --add                 添加一个新的变量:name value
    --unset               删除一个变量:name [value-regex]
    --unset-all           删除所有匹配项:name [value-regex]
    --rename-section      重命名小节:old-name new-name
    --remove-section      删除一个小节:name
    -l, --list            列出所有
    -e, --edit            打开一个编辑器
    --get-color           获得配置的颜色:配置 [默认]
    --get-colorbool       获得颜色设置:配置 [stdout-is-tty]

类型
    -t, --type <>         取值为该类型
    --bool                值是 "true" 或 "false"
    --int                 值是十进制数
    --bool-or-int         值是 --bool or --int
    --path                值是一个路径(文件或目录名)
    --expiry-date         值是一个到期日期

其它
    -z, --null            终止值是 NUL 字节
    --name-only           只显示变量名
    --includes            查询时参照 include 指令递归查找
    --show-origin         显示配置的来源(文件、标准输入、数据对象,或命令行)
    --default <取值>      使用 --get 参数,当缺少设置时使用默认值

$ git branch -h
usage: git branch [<options>] [-r | -a] [--merged | --no-merged]
   or: git branch [<options>] [-l] [-f] <branch-name> [<start-point>]
   or: git branch [<options>] [-r] (-d | -D) <branch-name>...
   or: git branch [<options>] (-m | -M) [<old-branch>] <new-branch>
   or: git branch [<options>] (-c | -C) [<old-branch>] <new-branch>
   or: git branch [<options>] [-r | -a] [--points-at]
   or: git branch [<options>] [-r | -a] [--format]

Generic options
    -v, --verbose         show hash and subject, give twice for upstream branch
    -q, --quiet           suppress informational messages
    -t, --track           set up tracking mode (see git-pull(1))
    -u, --set-upstream-to <upstream>
                          change the upstream info
    --unset-upstream      Unset the upstream info
    --color[=<when>]      use colored output
    -r, --remotes         act on remote-tracking branches
    --contains <commit>   print only branches that contain the commit
    --no-contains <commit>
                          print only branches that don't contain the commit
    --abbrev[=<n>]        use <n> digits to display SHA-1s

Specific git-branch actions:
    -a, --all             list both remote-tracking and local branches
    -d, --delete          delete fully merged branch
    -D                    delete branch (even if not merged)
    -m, --move            move/rename a branch and its reflog
    -M                    move/rename a branch, even if target exists
    -c, --copy            copy a branch and its reflog
    -C                    copy a branch, even if target exists
    -l, --list            list branch names
    --show-current        show current branch name
    --create-reflog       create the branch's reflog
    --edit-description    edit the description for the branch
    -f, --force           force creation, move/rename, deletion
    --merged <commit>     print only branches that are merged
    --no-merged <commit>  print only branches that are not merged
    --column[=<style>]    list branches in columns
    --sort <key>          field name to sort on
    --points-at <object>  print only branches of the object
    -i, --ignore-case     sorting and filtering are case insensitive
    --format <format>     format to use for the output

原文地址:https://www.cnblogs.com/msvc/p/11417387.html