Git 常用命令
1.仓库初始化与克隆
初始化本地仓库:
git init
克隆远程仓库:
git clone <repository-url>
2.文件状态与更改
查看文件状态:
git status
查看更改内容:
git diff
3.提交更改
添加文件到暂存区:
git add <file>
添加所有更改:
git add .
提交更改:
git commit -m "commit message"
4.分支操作
查看所有分支:
git branch
创建新分支:
git branch <branch-name>
切换分支:
git checkout <branch-name>
创建并切换到新分支:
git checkout -b <branch-name>
合并分支:
git merge <branch-name>
删除分支:
git branch -d <branch-name>
5.远程仓库操作
查看远程仓库:
git remote -v
添加远程仓库:
git remote add <remote-name> <url>
推送更改到远程仓库:
git push <remote-name> <branch-name>
拉取远程更改:
git pull <remote-name> <branch-name>
6.标签操作
查看所有标签:
git tag
创建标签:
git tag <tag-name>
推送标签到远程仓库:
git push origin <tag-name>
删除标签:
git tag -d <tag-name>
git push origin :refs/tags/<tag-name>
7.版本回滚
查看提交历史:
git log
回滚到指定提交:
git reset --hard <commit-hash>
撤销最近一次提交:
git reset --soft HEAD~1
8.其他常用命令
忽略文件:
创建或编辑 .gitignore 文件,添加需要忽略的文件模式。
查看远程仓库信息:
git remote show <remote-name>
拉取所有分支:
git fetch --all
查看文件历史更改:
git log -p <file>