20个常用Git命令,放在收藏夹里吃灰

195 阅读3分钟

1. git config

 #全局设置用户名和邮箱地址
 git config --global user.name "your name"
 git config --global user.email "email address"
 ​
 #示例
 git config --global user.name "大阳"
 git config --global user.email "295513890@qq.com"

2. git init

 #初始化新的项目仓库
 git init [project repository name]
 ​
 #示例
 git init demo

3. git clone

 #从url下载代码仓库
 git clone url
 ​
 #示例
 git clone https://github.com/openjdk/jdk17.git

4. git add

 #将文件添加到暂存区
 git add [file]
 #示例
 git add README.md
 ​
 #将文件夹下所有更改过得文件添加到暂存区
 git add *

5. git commit

 #将暂存区的内容提交到本地仓库
 git commit -m "commit log"
 #示例
 git commit -m "first commit"
 ​
 #使用git add将文件添加到暂存区,并提交到本地仓库
 git commit -a

6. git diff

 #比较还没有暂存的文件差异
 git diff
 #比较暂存区与最新版本之间的差异
 git diff --staged
 #比较分支之间的差异
 git diff [first branch] [second branch]

7. git reset

 #取消暂存文件,但会保留文件内容
 git reset [file]
 ​
 #撤消指定提交的所有提交,并在本地保留更改
 git reset [commitId]
 #示例,撤消提交
 git reset 8c104f5ce1b2b2964c464c4780246026a8f6e1e3
 ​
 #丢掉所有历史记录并返回到指定的提交
 git reset --hard [commitId]
 #示例
 git reset --hard 8c104f5ce1b2b2964c464c4780246026a8f6e1e3

8. git status

 #列出更改了的的所有文件
 git status
 ​
 #简化输出结果
 git status -s

9. git mv

 #移动或重命名一个文件、目录
 git mv [file] [newfile]
 ​
 #强制移动或重命名
 git mv -f [file] [newfile]

10. git rm

 #从工作目录中删除文件,并暂存删除状态
 git rm [file]
 #示例
 git rm README.md

11. git log

 #列出当前分支的版本日志记录
 git log
 ​
 #列出指定文件的版本历史记录,还包括文件的重命名
 git log --follow README.md

12. git show

 # 显示指定提交的元数据和更改内容
 git show [commitId]
 #示例
 git show 0869907c9cfdee3294924b82045beb0a4d40760b

13. git tag

 #为指定的提交打标签
 git tag [commitId]

14. git branch

 #列出仓库的所有本地分支
 git branch
 ​
 #创建一个新分支
 git branch [branch name]
 ​
 #删除分支
 git branch -d [branch name]

15. git checkout

 #切换分支
 git checkout [branch name]
 ​
 #创建一个新分支,并切换到新分支
 git checkout -b [branch name]

16. git merge

 #将指定分支的历史记录合并到当前分支中
 git merge [branch name]

17. git remote

 #将本地仓库连接到远程服务器
 git remote add [variable name] [remote server link]
 #示例
 git remote add origin https://github.com/openjdk/jdk17.git

18. git push

 #将主分支的已提交更改推送到远程仓库
 git push [variable name] master
 #示例
 git push origin master
 ​
 #将分支提交发送到远程仓库
 git push [variable name] [branch]
 #将所有分支推送到远程仓库
 git push --all [variable name]
 #示例
 git push --all origin
 ​
 #删除远程仓库上的分支
 git push [variable name] :[branch name]
 #示例
 git push origin: branch2

19. git pull

 #提取远程服务器上的更改并将其合并到工作目录中
 git pull [remote link]
 git pull https://github.com/openjdk/jdk17.git

20. git stash

 #临时存储所有已修改的跟踪文件
 git stash save
 ​
 #还原隐藏的文件
 git stash pop
 ​
 #列出隐藏的变更集
 git stash list
 ​
 #丢弃隐藏的变更集
 git stash drop

\