简介
Git是一种分布式版本控制系统,被广泛应用于软件开发中,用于追踪文件的变化并协调多人在同一项目上的工作。在本文中,我们将介绍一些常用的Git命令,帮助你更好地使用Git来管理代码版本。
一、 初始化与配置
-
初始化一个新仓库
git init
-
克隆远程仓库
git clone https://github.com/example/repo.git
-
配置用户信息
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
二、提交与修改命令
-
查看文件状态
git status
-
添加文件到暂存区
git add . #.代表所有文件都加到暂存区
-
提交到本地仓库
git commit -m "Commit message"
-
提交到本地仓库
git commit -m "Commit message"
三、分支管理命令
-
查看分支
git branch
-
创建新分支
git branch new_feature
-
切换分支
git checkout new_feature
-
创建并切换到新分支
git checkout -b new_feature
-
合并分支
git merge new_feature #将指定分支名称合并到当前分支
四、远程仓库操作命令
-
查看远程仓库
git remote -v
-
添加远程仓库
git remote add origin https://github.com/example/repo.git
-
拉取远程分支
git pull origin master
-
推送本地分支
git push origin new_feature
五、撤销与重置
-
撤销工作区修改
git checkout -- file.txt
-
撤销暂存区修改
git reset HEAD file.txt
-
恢复到指定提交
git reset --hard commit_hash
-
回滚最后一次提交
git revert HEAD