# 最最常用的Git命令

# 克隆远端服务器仓库到本地
git clone https://github.com/google/promises

# 初始化本地git仓库(本地文件夹没有使用git的话,有的标志是有隐藏的.git文件夹)
git init

# 添加本地所有的修改到暂存区(等待提交)
git add .

# or添加本地某个文件的修改到暂存区(等待提交)
git add <文件路径>

# 本地提交(等待推到远端服务器)
git commit -m "我改了XX文件(提交信息)"

# 推送本地的这次提交到远端服务器(良好的习惯是先拉再推,即先执行git pull)
git push

# 拉取远程服务器的最新代码
git pull

#切换到master分支,master一般是主分支,master有奴隶主的意思,现在为了避免种族歧视一般新建的都是main
git checkout master

#从master切换到gh-pages
git checkout gh-pages

# 还原某个文件
git checkout -- Project/Tool/XDHTTPManager.m

Command line instructions

# Git 常用命令

# stash:暂存

# 暂存本地已修改的文件,让工作区变得干干净净,以便拉取新代码
#暂存本地的修改
git stash save "your remark 你的备注"
# 应用上次暂存的修改操作
#应用上次的暂存,恢复暂存的文件
git stash apply stash@{0}

其他

# 删除上一个(最新的)暂存
git stash drop
# 删除所有的暂存
git stash clear

# branch 分支

#切换到master分支
git checkout master 
#在本地计算机上创建、切换到3.x分支,并且本地跟踪远程3.x分支
git checkout -b 3.x --track origin/3.x
#将分支推送到origin,origin就是github:
git push origin name_of_your_new_branch
#当官方存储库中的原始分支已更新时,更新您的分支:
git fetch name_of_your_remote
#把dev分支的工作成果合并到master分支上(此时要切换到master,即目标分支):
git merge dev
#删除dev分支
git branch -d dev
# 合并master到dev出现readme.md这个文件冲突时,修改readme.md文件,再在dev上解决冲突、提交到远程:
git add readme.md
git commit -m "conflict fixed"
git push origin dev
# 合并master到dev后想反悔(后退一步)?在dev上执行如下即可:
git reset --hard HEAD~1
git push -f origin dev
# 删除本地文件系统上的分支:
git branch -d name_of_your_new_branch

# 在本地master上新建dev分支并检出dev分支
git branch dev
git checkout dev


# SourceTree里面推送到远端(未测试)
git push -v --tags --set-upstream origin refs/heads/dev:refs/heads/dev 
# 撤销某一类的文件更改 https://stackoverflow.com/a/9345967/4493393
git checkout \*.{cpp,h}

# 拉取远端、提交

#拉取远端(服务器)的origin分支
git fetch origin 
#推送修改到远端(服务器)
git pull origin master 

# git pull强制覆盖本地文件

#从下面可以看到:此时本地有几个提交,但是没推送到远程,但是此时远程别人已经有很多次提交了,代码拉不下来git pull origin stable
From https://github.com/flutter/flutter
 * branch                stable     -> FETCH_HEAD
error: The following untracked working tree files would be overwritten by merge:
	XXXXXXXXXXXXXX 
Please move or remove them before you merge.
Aborting
#重置本地stable分支的修改git reset --hard origin/stable
Checking out files: 100% (2295/2295), done.
HEAD is now at 68587a091 V1.9.1 hotfix

# tag

# 新建tag,2ed3952e4ecea2c9808f2d58d339a6a52ee0978a 可由git log命令获取
git tag -a -m  1.0.0 2ed3952e4ecea2c9808f2d58d339a6a52ee0978a
git tag -a -m  1.20211110 c43faed41dbee9c0734c2dc6a407cb00b7defae7 

# 推送tag到origin
git push -v origin refs/tags/1.0.0 
git push -v origin refs/tags/1.20211110 

# 删除分支并推送修改到远程

git branch -d -r origin/homeBranch
git push origin :homeBranch 

# 查看远程仓库地址

git remote -v 

# Git 时光机

如果你的东西丢了,或者提交了不该提交的账号密码,你可以:

# 本地commit了还没push,想撤回这次commit

#撤回到上一次提交
git reset HEAD~

# 反悔之本地commit了且已push到远程,想把本地和远程的这次commit都撤销

Keyword:撤回、撤销、回退、重置

场景:不小心把代码里自己的账号密码提交到github,全世界的网友都能看到,如何取消呢?

#先看log里的hash
git log

#撤回到指定哈希值,比如git reset --hard e158090dfaff80d54c091aef1ce0d49666957c80
git reset --hard <commit-hash>

# 推送到远端,让远端更新,master可换成你的当前分支,也可以:git push -f
git push -f origin master

# 反悔之忘了pull代码本地就commit了,但是还没pull到远程服务器

# HASH是远程服务器别人提交的最新的哈希值
# --soft是软合并,保持本地所有改动,本地所有改动不会丢失
git reset -q --soft HASH

# 反悔之仅撤销远程master的更改

#先从log里找某个你想留下的好的记录的hash
git log

#仅撤销远程master的更改
git push -f origin e158090dfaff80d54c091aef1ce0d49666957c80:master

# 自己的分支跟dev分支保持最新

搜索关键字:git keep a branch update with master

# 切换到你的分支your_branch
git checkout your_branch
# 合并dev的最新代码到你自己的
git merge dev

# 撤销rebase

# 先通过`git reflog`找到自己上次rebase前的那次提交,比如我这边可以看到
# cf7a70c80 (origin/dev_pw) HEAD@{9}: commit: 替换获取相册图片方法
git reflog
# 9是你rebase之前的那次提交
git reset --hard HEAD@{9}

# --recurse-submodules

如果您已经克隆了项目并忘记了 --recurse-submodules, you can 组合 the git submodule init and git submodule update 这两步 by running git submodule update --init. 要初始化、获取和签出任何嵌套子模块,您可以使用简单的 git submodule update --init --recursive.

# eg:
git clone https://github.com/johnno1962/InjectionIII --recurse-submodules
#等价于
git clone https://github.com/johnno1962/InjectionIII.git
git submodule init
git submodule update

# 放弃所有本地更改(慎用)

使用场景:切换到上次提交,结果发现本地有部分文件更改导致无法切换,而这部分更改我也不想要

git reset -q --hard HEAD

# clone

克隆完整仓库:

git clone https://github.com/USERNAME/REPONAME.git

会出现一个名字叫REPONAME的文件夹,里面的内容如下

├── .git
├── code
├── docs
├── images
└── README.md

克隆某个分支且仅最新的一次提交:

git clone --depth 1 -b ppdev  https://gitlab.itophis.com:4001/WGHYYXM/app-mobile.git

# --bare

git clone --bare https://github.com/USERNAME/REPONAME.git

会出现一个名字叫REPONAME.git的文件夹,里面的内容如下

├── HEAD
├── config
├── description
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── fsmonitor-watchman.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── pre-merge-commit.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   ├── prepare-commit-msg.sample
│   ├── push-to-checkout.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
│       ├── pack-xxx.idx
│       └── pack-xxx.pack
├── packed-refs
└── refs
    ├── heads
    └── tags

# --mirror

git clone --mirror https://github.com/USERNAME/REPONAME.git

会出现一个名字叫REPONAME.git的文件夹,里面的内容同上,区别在于上面的config文件内容比加--bare多了最后两行:

[core]
	repositoryformatversion = 0
	filemode = true
	bare = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = https://github.com/USERNAME/REPONAME.git
	fetch = +refs/*:refs/*
	mirror = true

packed-refs文件比--bare多了最后一行:

# pack-refs with: peeled fully-peeled sorted 
037e259f744db25d8235ea2424212b418e1c6227 refs/heads/master
b8b84444a10dea0b02b373d854a468c305cade98 refs/pull/2/head

# 从本地仓库中删除远程及其引用

克隆别人的仓库后,想保留历史commit再存到自己的git服务器

git remote -v                                                                                   PPSticker/git/master
origin	https://github.com/miguelgrinberg/flasky (fetch)
origin	https://github.com/miguelgrinberg/flasky (push)
# git remote rm 不会从服务器中删除远程仓库。 它只是从本地仓库中删除远程及其引用git remote rm origin                                                                            PPSticker/git/master
❯ git remote -v

碰到的问题及解决方法:

$ git status
On branch dev
Your branch and 'origin/dev' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)
xxx git:(dev) $ git reset --hard origin/dev

# Git 高级

瘦身、清理大文件

https://rtyley.github.io/bfg-repo-cleaner/#usage

https://action121.github.io/2020/09/02/Git%E7%98%A6%E8%BA%AB.html

# Git配置

# 设置代理

v2ray https://github.com/v2ray/v2ray-core/issues/1190

# 检查有没有代理
git config --global --get http.proxy
git config --global --get https.proxy

# 设置http和https代理
git config --global http.proxy http://127.0.0.1:1087
git config --global https.proxy https://127.0.0.1:1087

# 取消设置http和https代理
git config --global --unset http.proxy && git config --global --unset https.proxy



# Xcode设置
/Applications/Xcode.app/Contents/Developer/usr/bin/git config --global http.proxy http://127.0.0.1:1087 && /Applications/Xcode.app/Contents/Developer/usr/bin/git config --global https.proxy https://127.0.0.1:1087
# Xcode 取消
/Applications/Xcode.app/Contents/Developer/usr/bin/git config --global --unset http.proxy && /Applications/Xcode.app/Contents/Developer/usr/bin/git config --global --unset https.proxy

# 查看本地配置

命令:

git config --list | grep user

结果:

user.name=panda
user.email=panda@example.com

# SourceTree选项详解

# 立即提交合并的改动

✅勾选

git pull origin master 

☑️不勾选

git pull --no-commit origin master 

# 用变基代替合并

✅勾选

git pull --rebase origin master 

☑️不勾选

git pull origin master 
git --no-optional-locks -c color.branch=false -c color.diff=false -c color.status=false -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree XXXXXXXXXXXXXXX 

# 初始化本地或远程Git

# Git 全局设置global setup

git config --global user.name "panda" 
git config --global user.email "panda@example.com" 

# 创建远程git仓库

提交推送到远程之先有远程git仓库,后有本地git仓库

git clone https://github.com/Panway/PandaNote.git 
cd pandanote 
touch README.md 
git add README.md 
git commit -m "add README" 
git push -u origin master

# 本地git初始化并提交到远程

适合本地没初始化git的情况

提交推送到远程之先有本地git,后创建远程git仓库的情况

cd pandanote 
git init 
git remote add origin https://github.com/Panway/PandaNote.git
git add . 
git commit -m "Initial commit" 
git push -u origin master

# Existing Git repository

???

cd existing_repo 
git remote rename origin old-origin
git remote add origin https://github.com/Panway/PandaNote.git
git push -u origin --all 
git push -u origin --tags

GitLab + Jenkins 类似的技术方案推荐

https://www.v2ex.com/t/835915

上次更新: 2/27/2022, 10:30:33 PM