Git基本配置

安装Git后需要配置用户名和邮箱,这些信息会出现在提交记录中。
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

查看当前配置:
git config --list

仓库初始化与克隆

本地初始化新仓库:
git init

克隆远程仓库到本地:
git clone <repository_url>

基础文件操作

查看当前文件状态:
git status

将文件添加到暂存区:
git add <file_name>
git add .(添加所有变更)

提交变更到本地仓库:
git commit -m "commit message"

分支管理

查看分支列表:
git branch

创建新分支:
git branch <branch_name>

切换分支:
git checkout <branch_name>

创建并切换分支:
git checkout -b <branch_name>

删除分支:
git branch -d <branch_name>

远程协作

查看远程仓库:
git remote -v

添加远程仓库:
git remote add <remote_name> <repository_url>

推送本地分支到远程:
git push <remote_name> <branch_name>

拉取远程更新:
git pull <remote_name> <branch_name>

版本回退

查看提交历史:
git log

回退到指定版本:
git reset --hard <commit_id>

撤销工作区修改:
git checkout -- <file_name>

合并与冲突解决

合并分支:
git merge <branch_name>

出现冲突时手动解决冲突文件,然后标记为已解决:
git add <resolved_file>
git commit

储藏临时修改

临时保存工作区修改:
git stash

恢复储藏的内容:
git stash pop

标签管理

创建标签:
git tag <tag_name>

推送标签到远程:
git push <remote_name> <tag_name>

删除标签:
git tag -d <tag_name>

高级操作

修改最近一次提交:
git commit --amend

交互式变基:
git rebase -i <commit_id>

查看文件修改差异:
git diff


 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐