$ git init [项目名]
$ git clone git_url
$ git clone git_url my_directory
$ git status
$ git add [文件名]
$ git add .
$ git commit -m "提交信息"
$ git commit -am "提交信息"
$ git restore [文件名]
$ git restore --staged [文件名]
$ git reset [文件名]
$ git reset --hard
$ git diff
$ git diff --staged
$ git rebase [分支]
$ git config --global user.name "用户名"
$ git config --global user.email "邮箱"
$ git config --global color.ui auto
$ git config --global --edit
$ git branch
$ git branch -av
$ git checkout my_branch
$ git checkout -b new_branch
$ git branch -d my_branch
$ git checkout branchB
$ git merge branchA
$ git tag my_tag
$ git log
$ git log branchB..branchA
$ git log --follow [文件名]
$ git diff branchB...branchA
$ git show [SHA]
$ git fetch [别名]
$ git merge [别名]/[分支]
# 不使用快进
$ git merge --no-ff [别名]/[分支]
# 仅使用快进
$ git merge --ff-only [别名]/[分支]
$ git push [别名] [分支]
$ git pull
$ git cherry-pick [提交ID]
$ git remote add [别名] [远程仓库URL]
$ git remote
$ git remote -v
$ git remote rm [远程仓库名]
$ git remote set-url origin [git_url]
$ git stash
$ git stash list
$ git stash pop
$ git stash drop
$ git rm [文件名]
$ git mv [原路径] [新路径]
$ git log --stat -M
在 .gitignore
文件中,指定不需要 Git 跟踪的文件
/logs/*
# "!" 表示不忽略
!logs/.gitkeep
# 忽略 Mac 系统文件
.DS_store
# 忽略 node_modules 文件夹
node_modules
# 忽略 SASS 配置文件
.sass-cache
$ git branch -m <新名称>
$ git push origin -u <新名称>
$ git push origin --delete <旧名称>
$ git log -S'<源代码中的某个术语>'
$ git log -p <文件名>
$ git log --pretty=oneline --graph --decorate --all
$ git branch -vv
$ git checkout -
$ git branch -r
$ git checkout <分支> -- <文件名>
$ git commit --amend -m "提交信息"
$ git commit --amend --no-edit
$ git commit --amend --no-edit --date="now"
$ git rebase -i HEAD~N # N是要修改的提交数量
在交互式 rebase 的编辑界面,使用 squash
或 fixup
合并提交。
pick e3a1b35 提交1
squash f98f27e 提交2
$ git push --force
$ git config --list
$ git clean -fd
$ git init --bare
$ git hash-object [文件名]
$ git log --oneline
首先生成 SSH 密钥:
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
然后将公钥添加到 GitHub 的 SSH 设置中。
$ git clone git@github.com:username/repository.git
$ git clone https://github.com/username/repository.git
$ git push origin main
$ git status
冲突标记如下:
<<<<<<< HEAD
代码块A
=======
代码块B
>>>>>>> branch-name
手动编辑并解决冲突后,使用 git add
将文件添加到暂存区。
$ git commit
$ git merge --abort
$ git config --global alias.st status
$ git config --global alias.ci commit
$ git config --global alias.co checkout
这样,你就可以使用 git st
来代替 git status
,git ci
来代替 git commit
,以此类推。
$ git config --get-regexp ^alias
$ git config --global --unset alias.st
Git 支持多种钩子(hook),如在提交前检查代码或在推送之前进行某些操作。这些钩子位于 .git/hooks
目录中。你可以编辑这些文件来定制 Git 的行为。
$ cd .git/hooks
$ ls
例如,启用一个 pre-commit
钩子,可以创建一个 pre-commit
文件并加上脚本内容。Git 会在每次提交前运行该脚本。
$ git pull origin main
在开始新功能时,应该为每个新功能创建一个新的分支:
$ git checkout -b feature/new-feature
--no-ff
防止丢失历史使用 --no-ff
选项来强制 Git 创建一个新的合并提交,保留分支合并的历史。
$ git merge --no-ff feature/new-feature
确保在开发过程中,适时地提交更改。每个提交应该是功能完整的,并且具有清晰的提交信息。
如果多个开发者修改了相同的代码段,合并时可能会出现冲突。此时需要手动解决冲突,确保代码正确性。
$ git submodule add [repository_url] [路径]
$ git submodule update --init
$ git submodule update --remote
$ git submodule deinit [路径]
$ git rm [路径]
$ rm -rf .gitmodules
$ git lfs install
$ git lfs track "*.psd"
$ git add .gitattributes
$ git add <大文件>
$ git commit -m "Add large file"
$ git gc --aggressive
删除本地和远程不再使用的分支,以保持仓库的整洁和性能。
$ git branch -d <分支名>
$ git push origin --delete <分支名>
以上为 Git 命令参考手册的扩展部分,包含了从基础操作到高级功能的全面讲解。您可以根据需要查阅和使用相应的命令和技巧。
Git
有了更深入的理解和认识。