Git学习
Git常用命令
-
在本地目录下新建git项目仓库:
git init
(后面可加工程文件名,会自动创建) -
克隆远端工程到本地仓库:
git clone [URL]
或git lfs clone [URL]
(lfs对二进制文件支持较好) -
新增/删除/移动文件到暂存区:
git add/git rm
(也可从硬盘删除文件,再git commit
)/git mv
-
比较任意两个版本(分支)的差异或当前与上次提交的差异:
git diff
举例: 比较两个节点之间的差异:git diff f123 f456 比较两个分支之间的差异:git diff master branch 当前索引和上次提交间的差异:git diff --cached 只看文件列表:在diff后面加--name-statue参数
-
显示工作目录和暂存区的状态:
git status
-
将暂存区里的文件改动提交到本地的版本库:
git commit file_name -m "commit message"
一次性提交所有在暂存区改动的文件到版本库:
git commit -am "commit message"
提交成功后,git日志可以查看提交id和描述信息:
git log
-
查看提交历史:
git log
常见的参数:
--name-status/-p/--pretty/--graph
-
推送到远端仓库:
git push origin branch_name
本地分支名可以与远端分支名不同:
git push origin branch_name:new_branch_name
注意:Windows下的分支名大小写不敏感
-
查看本地工程的所有git分支名称:
git branch
,返回结果中*表示当前所在分支查看远端服务器上的分支:
git branch -r
,返回的分支带origin前缀,表示在远端查看远端和本地的所有分支:
git branch -a
-
新建分支:
git branch branch_name
,不会切换到新分支新建分支:
git check -b branch_name
,新建后自动切换到新分支删除分支:
git branch -d/git branch -D branch_name
(D强制删除)删除远程服务器分支:
git branch -d -r branch_name
注意 :删除后需要推送到服务器:
git push origin : branch_name
切换分支:
git checkout branch_name
(强制切换git checkout -f
) -
从远程服务器获取某个分支的更新,并与本地指定的分支合并:
git pull origin remote_branch:local_branch
远端分支与本地分支相同:
git pull origin remote_branch
从远端服务器获取分支更新到本地仓库(与git pull不同,不会进行自动合并):
git fetch origin remote_branch:local_branch
远端分支与本地分支相同:
git fetch origin remote_branch
-
从指定的分支(节点)合并到当前分支:
git merge branch_name
合并目标分支内容到当前分支:
git rebase branch_name
-
撤销当前工作区某些
git add/commit
操作,退回到历史提交节点:git reset commit_id
回退本地所有修改而未提交的文件内容:
git checkout .
仅仅回退某个文件:
git checkout -filename
将工具区回退到某个提交版本:
git checkout commit_id