Git command - summary of common commands

1. git config common commands

//获取git config命令信息
git config -h
//获取system、global、local配置信息
git config --list
//获取global配置信息
git config --global --list
//配置global用户名和邮箱
$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"
git config --global user.email "[email protected]"
//查看global用户名和邮箱,--get命令可省略
$ git config --global user.name
$ git config --global user.email
//为git branch命令添加alias,简化命令。
//添加后可以使用git br替代git branch命令
$ git config --global alias.br "branch"
//配置了两个相同的别名解决:
$ git config --global --replace-all alias.br "branch"
//取消别名配置
$ git config --global --unset alias.br

//查看配置信息,并显示配置信息所在文件路径、所属级别(如global)
$ git config --list --show-origin --show-scope
//列出所有的git配置,只显示变量名称,不显示值
$ git config --list --name-only

2. git add common commands

//将指定文件添加到git仓库
git add filename

//将目录下所有文件添加到git仓库
$ git add .
$ git add -A

3. Common commands for git commit

//提交 对于修改后的文件,需要先add再commit -m
$ git commit -m 'message'
//提交所有更改的信息
$ git commit -a //需要在控制台继续输入commit message
//对于已经提交过的文件(处于已跟踪(tracked)状态),相当于git add + git commit -m
$ git commit -a -m 'message'

//在不指定提交消息的情况下提交更改
$ git commit --allow-empty-message -m ''

4. git reset command

//什么是暂存区?
//项目目录可以看作是一个工作区,对项目中所有文件的修改都是在工作区进行;
//执行git add命令后,会将修改的文件放到暂存区;
//执行git commit命令后,会将修改的文件更新到版本库,同时清空暂存区内容。

//不小心改乱了工作区内容,如何撤销?
$ git checkout -- filename

//不小心改乱了工作区内容,并且执行git add命令添加到了暂存区,如何撤销?
//1.先执行 git reset HEAD filename,撤销暂存区的修改
$ git reset HEAD filename
//2.撤销工作区修改
$ git checkout --filename

//已经提交了不合适的修改到版本库时,如何撤销?
$ git reset --hard commit_id

//--soft:仅仅移动版本库HEAD指针,暂存区、工作区不会重置 //--mixed:reset默认的,移动版本库HEAD指针,重置暂存区,但不重置工作区。
//--hard:移动版本库HEAD指针,重置暂存区和工作区。 

5. git log common commands

//git提交记录
$ git log
//git提交记录,显示每个文件内容的修改记录
git log -p
//git提交记录,显示每次提交修改的文件
git log --stat
//commitId只显示前几位
git log --abbrev-commit
//显示ASCII图形表示的分支合并历史
--graph
//自定义日期显示格式
$ git log --date=format:'%Y-%m-%d %H:%M:%S'
//控制显示的记录格式:%h-提交对象的简短哈希字串,%Cred-字体颜色为红色
--pretty=format:'%Cred%h'

//显示前10条log
git log -10  
//显示2022年01月01号及之后的commit
git log --after="2022-01-01" 
//显示2022年01月01号之前的commit
git log --before="2022-01-01"
//显示某开发者的提交记录
git log --author="开发者"
//根据commit描述,查找对应的提交记录
//可以传入-i用来忽略大小写
//如果想同时使用--grep和--author,必须在附加一个--all-match参数
git log --grep="Initial commit"
//查看某个文件相关的提交记录
$ git log -- test.txt test1.txt
//查看file目录下所有文件提交记录
$ git log -- file/
//查看dev分支的提交记录
$ git log dev --
//查看“Hello World!”这段内容的提交记录
git log -S'Hello World!'
//如果想使用正则表达式去匹配而不是字符串,可以使用-G代替-S
git log -G'*Hello'
//在feature有而在master没有的所有commit
$ git log master..feature
//过滤掉merge commit
git log --no-merges
//查看merger
git log --merges
//查看 v1.0标签记录
git log v1.0
//查看v1.0标签之后的提交记录
git log v1.0..
//查看commit a08ab之前的记录
git log a08ab
//查看ao8ab和da80d之间的记录
git log ao8ab da80d

//自定义日志输出格式
$ git config --global alias.lg "log --graph --stat --color --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%Cred%h%Creset -%C(yellow)%d%C(bold reset) %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --abbrev-commit"

//为自定义的git日志输出格式配置alias
$ git config --global alias.lg "log --graph --stat --color --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%Cred%h%Creset -%C(yellow)%d%C(bold reset) %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --abbrev-commit"

6. Common command of git branch

//查看本地分支
$ git branch
//查看远程分支
$ git branch -r
//查看全部分支
$ git branch -a
//创建并切换到分支
$ git checkout -b <分支名称>
//切换分支
$ git checkout <分支名称>
//删除本地分支
$ git branch --delete <分支名称>
//删除远程分支
$ git push origin --delete <分支名称>
//分支重命名
$ git branch -m <旧分支名称> <新分支名称>
//合并feature分支到当前分支
$ git merge feature

7. git diff common command

//比较某个文件和当前git版本库的区别
$ git diff test.txt
//比较两个分支的不同
$ git diff branch1..branch2

8. Git remote common commands

//生成密钥
ssh-keygen -t rsa -C "[email protected]"
//查看密钥
cd ~/.ssh
cat id_rsa.pub

//添加一个远程仓库
$ git remote add <remote_name> <remote_url>
//列出所有远程仓库及其URL
$ git remote -v
//删除远程仓库
$ git remote rm <remote_name>
//重命名远程仓库
$ git remote rename <old_name> <new_name>

//在origin中添加远程仓库地址
git remote set-url --add origin <gitlab仓库url>

9. git pull common command

//将远程更改合并到当前分支
$ git pull

//使用rebase合并远程更改
//在将您的提交推送到远程仓库之前,Git会在最新更改的顶部重新应用每个提交。
//这确保了提交的线性时间线,简化了故障排除和调试过程
$ git pull --rebase

10. git push common command

$ git push [远程库名称] [分支名称]
建立远端映射
$ git push --set-upstream origin <分支名称>

11. git clone common command

//clone一个仓库
$ git clone <存储库url>
//clone特定分支
$ git clone --branch <分支名> <存储库url>
//clone特定标签
$ git clone --branch <标签名> <存储库url>
//clone特定提交
$ git clone --branch <提交哈希值> <存储库url>
//clone子目录
$ git clone --depth 1 --filter=blob:none https://github.com/my-username/my-repository.git my-subdirectory

at last

If you want to become an architect or want to break through the 20-30K salary range, then don't be limited to coding and business, but you must be able to select models, expand, and improve programming thinking. In addition, a good career plan is also very important, and the habit of learning is very important, but the most important thing is to be able to persevere. Any plan that cannot be implemented consistently is empty talk.

If you have no direction, here I would like to share with you a set of "Advanced Notes on the Eight Major Modules of Android" written by the senior architect of Ali, to help you organize the messy, scattered and fragmented knowledge systematically, so that you can systematically and efficiently Master the various knowledge points of Android development.
img
Compared with the fragmented content we usually read, the knowledge points of this note are more systematic, easier to understand and remember, and are arranged strictly according to the knowledge system.

Welcome everyone to support with one click and three links. If you need the information in the article, you can directly scan the CSDN official certification WeChat card at the end of the article to get it for free↓↓↓

PS: There is also a ChatGPT robot in the group, which can answer your work or technical questions
picture

Guess you like

Origin blog.csdn.net/YoungOne2333/article/details/131710453