常用命令汇总
常用操作
提交更改的文件
git commit file_name -m "修改信息" # 附上了提交信息
git commit -a m "修改信息" # 一次性提交所有暂存文件
推送至远端仓库
git push origin branch_name # 将本地名为branch_name的分支推送到远端
git push origin branch_name: new_branch_name # 本地分支名称与推送远端的分支名不同
分支管理
查看分支
git branch # 查看本地分支
git branch -r # 查看远端拥有哪些分支,返回的分支带origin的表示在远端
git branch -a # 查看本地和远端所有分支
创建分支
git branch new_branch_name # 创建名为new_branch_name,并不会切换到新的分支
git checkout -b branch_name # 新建分支后自动切换到新的分支
从远端分支更新本地分支
git pull origin remote_branch:local_branch # 从远端获取某个分支的更新到本地仓库
git pull origin remote_branch # 如果远端和本地分支一样,可以省略:local_branch
git fetch origin remote_branch # 在获取到更新后,并不会进行合并
删除分支
git branch -d branch_name # 删除本地分支
# 删除远端分支
git branch -d -r branch_name
git push origin:branch_name # 删除后需要推送到服务器
合并分支
# 合并branch_name分支到当前分支
git merge branch_name
git rebase branch_name
撤销操作
# (谨慎使用)
git reset commit_id # 撤销当前工作区中的git add/commit操作
git checkout . # 回退本地所有已修改但未提交的内容
# 小范围撤销
git checkout -filename # 如果仅仅想回退某个文件的已修改未提交的内容
git checkout commit_id # 回退到某个提交版本