1. 初始化本地仓库
git init <directory>
这是可选的。如果未指定,将使用当前目录。<directory>
2. 克隆远程存储库
git clone <url>
3. 将文件添加到暂存区域
git add <file>
要添加当前目录中的所有文件,请使用代替。.
<file>
git add .
4. 提交更改
git commit -m "<message>"
如果要添加对跟踪文件所做的所有更改并提交
git commit -a -m "<message>"
# or
git commit -am "<message>"
5. 从暂存区域删除文件
git reset <file>
6. 移动或重命名文件
git mv <current path> <new path>
7. 从存储库中删除文件
git rm <file>
您也可以仅使用标志将其从暂存区域中删除--cached
git rm --cached <file>
基本 Git 概念
- 默认分支名称:
main
- 默认远程名称:
origin
- 当前分支引用:
HEAD
- 父项:或
HEAD
HEAD^
HEAD~1
- 祖父母:或
HEAD
HEAD^^
HEAD~2
13. 显示分支
git branch
有用的标志:
-a
:显示所有分支(本地和远程)-r
:显示远程分支-v
:显示上次提交的分支
14. 创建分支
git branch <branch>
您可以创建一个分支并使用命令切换到它。checkout
git checkout -b <branch>
15. 切换到分支机构
git checkout <branch>
16. 删除分支
git branch -d <branch>
您还可以使用 flag强制删除分支。-D
git branch -D <branch>
17. 合并分支
git merge <branch to merge into HEAD>
有用的标志:
--no-ff
:创建合并提交,即使合并解析为快进--squash
:将指定分支中的所有提交压缩为单个提交
快进合并
非快进合并
建议不要使用该标志,因为它会将所有提交压缩到单个提交中,从而导致混乱的提交历史记录。--squash
18. 变基分支
变基是将一系列提交移动或组合到新的基本提交的过程
git rebase <branch to rebase from>
19. 签出以前的提交
git checkout <commit id>
20. 还原提交
git revert <commit id>
21. 重置提交
git reset <commit id>
You can also add the flag to delete all changes, but use it with caution.--hard
git reset --hard <commit id>
22. Check out the status of the repository
git status
23. Display the commit history
git log
24. Display the changes to unstaged files
git diff
You can also use the flag to display the changes to staged files.--staged
git diff --staged
25. Display the changes between two commits
git diff <commit id 01> <commit id 02>
26. Stash changes
The stash allows you to temporarily store changes without committing them.
git stash
You can also add a message to the stash.
git stash save "<message>"
27. List stashes
git stash list
28. Apply a stash
Applying the stash will NOT remove it from the stash list.
git stash apply <stash id>
If you do not specify the , the latest stash will be applied (Valid for all similar stash commands)<stash id>
You can also use the format to apply a stash (Valid for all similar stash commands)stash@{<index>}
git stash apply stash@{0}
29. Remove a stash
git stash drop <stash id>
30. Remove all stashes
git stash clear
31. Apply and remove a stash
git stash pop <stash id>
32. Display the changes in a stash
git stash show <stash id>
33. Add a remote repository
git remote add <remote name> <url>
34. Display remote repositories
git remote
Add a flag to display the URLs of the remote repositories.-v
git remote -v
35. Remove a remote repository
git remote remove <remote name>
36 Rename a remote repository
git remote rename <old name> <new name>
37. Fetch changes from a remote repository
git fetch <remote name>
38. 从特定分支获取更改
git fetch <remote name> <branch>
39. 从远程存储库中提取更改
git pull <remote name> <branch>
40. 将更改推送到远程存储库
git push <remote name>
41. 将更改推送到特定分支
git push <remote name> <branch>
这就是所有人!🎉