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 "[email protected]"
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 "[email protected]" -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 [email protected]

git clone [email protected]:xxx/xxxx.git

ssh-agent bash -c 'ssh-add /home/me/my_private_key; git clone [email protected]:uname/test-git-repo.git'
  • 拉取分支:
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 -- 
  • 取消已经暂存的文件。即,撤销先前"git add"的操作
git reset HEAD ...
  • 修改最后一次提交。用于修改上一次的提交信息,或漏提交文件等情况。
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 [email protected]:git_username/repository_name.git
  • 切换关联的远程分支
git remote remove origin
git branch --set-upstream-to origin/develop develop
//或者
git remote add origin [email protected]:git_username/repository_name.git

❀子模块操作

  • 添加子模块
git submodule add  
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 命令

$ git branch -h
usage: git branch [] [-r | -a] [--merged | --no-merged]
   or: git branch [] [-l] [-f]  []
   or: git branch [] [-r] (-d | -D) ...
   or: git branch [] (-m | -M) [] 
   or: git branch [] (-c | -C) [] 
   or: git branch [] [-r | -a] [--points-at]
   or: git branch [] [-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 
                          change the upstream info
    --unset-upstream      Unset the upstream info
    --color[=]      use colored output
    -r, --remotes         act on remote-tracking branches
    --contains    print only branches that contain the commit
    --no-contains 
                          print only branches that don't contain the commit
    --abbrev[=]        use  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      print only branches that are merged
    --no-merged   print only branches that are not merged
    --column[=