1、config
临时修改用户名和邮箱(只修改当前仓库的提交者信息)
git config user.name '新用户名'
git config user.email '新邮箱地址'
查看配置命令
git config user.name
git config user.email
全局修改用户名和邮箱
git config --global user.name '新用户名'
git config --global user.email '新邮箱地址'
查看配置命令
git config --global user.name
git config --global user.email
2、add
将文件修改添加到暂存区(Staging Area)
git add <filename> 将指定文件添加到暂存区
git add . 将当前目录所有修改文件添加到暂存区(包括新增、修改、删除文件)
git add -A 或 git add --all 将所有修改文件添加到暂存区(包括子目录中的文件)
查看暂存区状态
git status 查看当前工作区暂存区的状态
git diff --cached或git diff --staged查看暂存区与最近一次提交之间的差异
丢弃已暂存的修改
git reset HEAD
丢弃指定已暂存的修改 git reset HEAD <filename>
3、 commit
将暂存区的修改提交到本地版本库,形成一个新的commit
git commit -m '提交说明' 提交暂存区的内容及提交说明
4、push
将本地版本库提交到远程版本库
git push origin <branch_name> 将本地分支提交到远程分支
5、remote
远程地址的相关操作
git remote -v 查看远程地址
git remote set-url origin <new-url> 修改远程地址
git remote add <name> <new-url> 添加远程地址(保留旧的添加新的)
git remote remove <name> 删除远程地址
6、撤销
撤销指定commit
git log查看提交记录
git revert <hash>
撤销指定commit之后的所有操作
git log查看提交记录
git reset --soft <commit> 撤销commit及其之后的所有提交,但保留更改在暂存区
git reset --mixed <commit> 同理 git reset <commit>
撤销指定提交及其之后的所有提交,将保留更改在工作区
git reset --hard <commit> 撤销指定提交及其之后所有提交,并丢失所有更改
--hard模板需谨慎使用,之后需要强制推送(git push --force)
撤销多个连续commit
git log查看提交记录
git revert <commit1>..<commit2> 撤销从提交 commit1到提交 commit2 的所有提交
7、checkout
在 Git 2.23 之后,引入了两个更明确的命令:git switch 和 git restore,用于替代部分 git checkout 和 git restore 的功能
切换分支 git checkout <branch-name> git switch <branch-name>
切换并创建新分支 git checkout -b <new-branch-name> git switch -c <new-branch-name>
恢复文件到特定版本或提交的状态 git checkout <commit-hash> -- <file-path>
恢复某个文件的内容到最近一次提交的状态 git restore <file>
恢复多个文件的内容到最近一次提交的状态 git restore <file1> <file2>
恢复整个工作区的所有文件到最近一次提交的状态 git restore .
恢复文件的内容到指定提交的状态 git restore --source=<commit> <file>
8、cherry-pick
将其他分支或提交历史中的某一个或多个特定的提交应用到当前分支
git cherry-pick <commit-hash>
git cherry-pick <commit1> <commit2> <commit3>
如果在cherry-pick过程中遇到冲突,需手动解决,然后运行git cherry-pick --continue,如果不想继续,运行git cherry-pick --abort取消此次操作
9、merge
将其他分支的修改合并到当前分支
git merge <branch_name>
git merge --abort 在合并过程中发生冲突且无法解决,可以使用此命令取消合并
git merge --no-ff <branch_name> 非快进合并,将合并历史记录保留下来
10、branch
git branch 列出所有本地分支
git branch -a 列出所有本地和远程分支
git branch <branch-name> 创建新分支
git branch -d <branch-name> 删除本地分支
git branch -D <branch-name> 强制删除本地分支
git push --delete <remote> <branch-name> 删除远程分支
git branch -m <new-branch-name> 重命名当前分支
git branch -m <old-branch-name> <new-branch-name> 重命名特定分支
git branch -u <remote-branch> 设置分支的上游分支
git branch -vv 查看分支的上游分支