常用Git命令解析

git

1
2
3
4
5
6
git init
git clone https://github.com/Username/repo.git
git add *
git commit -m "这是上传第一次项目"
git remote add origin https://gitlab.freedesktop.org/Username/test.git
git push -u origin master

查看

1
2
3
4
5
6
7
8
9
10
11
12
git log:	--oneline #查看历史记录的简洁的版本
--graph #查看历史中什么时候出现了分支、合并。
--reverse #逆向显示所有日志。
--author="Freedom Liu" #查找指定用户的提交日志
--since 和--before || --until和--after #指定日期
git log --oneline --after={2.weeks.ago} --before={2021-4-2}
git log filename #查看指定文件相关的commit记录
git log -p filenam #显示每次提交的diff
git blame <file> #查看指定文件的修改记录
git status #用于查看在你上次提交之后是否有对文件进行再次修改。
git show commit_id/tag #查看commit内容
git diff branch1 branch2 #比较分支

配置

1
2
3
4
5
6
7
8
9
10
11
git config --global user.name "Freedom Liu"
git config --global user.email "tianyu28658@gmail.com"
git config --global credential.helper store #记录下次输入的用户名和密码
git config --system --unset credential.helper #清空用户名和密码
git config --global core.editor vim #更换git默认编辑器
# 设置本地代理
git config --global http.proxy 'socks5://127.0.0.1:7890'
git config --global https.proxy 'socks5://127.0.0.1:7890'
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

版本

1
2
3
4
git tag
git tag -a 1.0.0 -m "version 1.0.0 add MHI support"
git push -u origin 1.0.0
git ls-remote --tag

远程库

1
2
3
4
5
6
git remote -v             # 显示所有远程仓库
git remote show [remote] # 显示某个远程仓库的信息

git remote add [shortname] [url] # 添加远程版本库
git remote set-url origin [url] # 更新远程仓库地址
git pull --rebase origin master # 把远程库中更新合并到本地库中

修改

1
2
3
# 不保留本地修改,用远程强制覆盖更新本地分支
git fetch --all
git reset --hard origin/master
1
2
3
4
5
6
# 删除远程文件:
git rm -r -n --cached 文件/文件夹名称
# (-n执行命令时,是不会删除任何文件,而是展示此命令要删除的文件列表预览)
# (--cached文件从暂存区域移除,但仍保留在当前工作目录中)
git commit -m "提交说明"
git push origin branch
1
2
3
4
5
6
7
# 追加到最后一次commit
# 如果只是单纯的追加到最后一次commit,可以直接commit之后使用rebase进行commit的合并:
git add .
git commit -m "add to the last commit"
git rebase -i HEAD~3
# change last commit flag to fix, then "Ctrl+C", type ":wq!" and "return"
git push -f origin master
1
2
3
4
5
6
7
8
9
# 在本地新建一个temp分支,并将远程origin仓库的master分支代码下载到本地temp分支
git fetch origin master:tmp
# 来比较本地代码与刚刚从远程下载下来的代码的区别
git diff tmp
# 合并temp分支到本地的master分支
git merge tmp
# 如果不想保留temp分支 可以用这步删除
git branch -d temp
git pull #更新本地分支
1
2
3
4
5
6
# 修改commit
git rebase -i commit_ID
# 跳过有冲突提交
git rebase --skip
# 中止并返回到“ git rebase”之前的状态
git rebase --abort
1
2
3
4
5
6
# gitlab 上fork别人的代码后,怎样去更新别人的新代码
git remote add upstream URL
git fetch upstream
git merge upstream/master
# 合并uptream上新的commit:
git pull --rebase upstream master
1
2
3
4
5
6
7
# Checkout locally by modifying .git/config for a given repository
git config -e
[remote "origin"]
url = https://gitlab.com/gitlab-org/gitlab-foss.git
fetch = +refs/heads/*:refs/remotes/origin/*
fetch = +refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*
git checkout origin/merge-requests/1

Patch

1
2
3
4
5
6
7
patch:
$ git format-patch HEAD^      #生成最近的1次commit的patch
$ git format-patch HEAD^^     #生成最近的2次commit的patch
$ git format-patch <r1>..<r2> #生成两个commit间的修改的patch(包含两个commit. <r1>和<r2>都是具体的commit号)
$ git format-patch -1 <r1> #生成单个commit的patch
$ git format-patch <r1> #生成某commit以来的修改patch(不包含该commit)
$ git format-patch --root <r1>  #生成从根到r1提交的所有patch

issues

1
2
3
4
# error: The following untracked working tree files would be overwritten by checkout:
git clean -d -fx
# error: failed to push some refs to
git pull --rebase origin master