Git(分布式版本控制系统)一些常用命令以及用法

191 阅读8分钟

Git

常用命令

  • create
	git clone // 克隆( clone an existing repository)
	git init // 初始化 (create a new local repository)
  • local changes
	git status // 查看文件修改状态(changed files in your working directory)
	git diff  // 查看修改内容 (changes to tracked files) 
	git add .  // 所有文件添加到暂存区(add all current changes to the next commit)
	git add <file> // 某个文件添加到暂存区( add some changes in <file> to the next commit)
	git commit -m 'message' // 提交到版本库 -m 后加提交的信息
  • commit history
	git log // 查看提交历史(show all commits, starting with newest)
	git log --graph // 查看分支合并图
	git relog // 查看命令历史
  • branches & tags
	git branch -a //查看所有分支(包括本地和远程)(list all existing branches)
	git branch // 查看所有本地分支
	git branch < name> // 创建一个新的本地分支
	git branch -d <name> // 删除某个分支
	git branch -D <name>  // 强制删除某个分支
	git checkout <name> // 切换到某个分支
	git checkout -b <name> // 创建并切换到某个分支
	git switch <name> //创建并切换到某个分支
	git tag <tagName> // 创建一个标签
  • update & publish
	git remote -v // list all currently configured remotes
	git remote show <remote> // show information about a remote
	git fetch <remote> // download all changes form <remote>,but don't integrate into HEAD
	git pull <remote> <branch> // download changes form directly merge/integrate into HEAD
	git push <remote> <branch> // publish local changes on a remote
	git push --tags // publish your tags

==git fetch 和 git pull的区别== git fetch:相当于是从远程获取最新版本到本地,不会自动merge git pull:相当于是从远程获取最新版本并merge到本地

  • merge & rebase
	git merge <branch>  //把某个分支合并到当前分支下( merge <branch> into your current HEAD)
	git rebase <branch> // rebase your current HEAD onto <branch>
  • undo
	git checkout --file // 撤销某个文件的修改
	git reset --hard HEAD // 版本回退到上个版本(discard all local changes in your working directory)
	git reset --hard commit_id // 版本回退到某个commit ID
	git revert <commit> // 撤销 某次操作
	

参考链接: liaoxuefeng.com

常用命令的用法

创建
  1. 初始化一个Git仓库,使用git init命令。 git init
  2. 克隆一个Git仓库,使用git clone 命令 git clone <地址链接>
本地相关操作
  1. 添加文件到Git仓库,分两步:
  • 使用命令git add < file >,注意,可反复多次使用,添加多个文件,其中git add . 代表添加所有文件;
  • 删除一个文件,手动删除文件,然后使用git rm < file>和git add< file>效果是一样的
  • 使用命令git commit -m ,完成。
	git add .
	git commit -m '本次提交的说明'
  1. 查看工作区状态
  • git status告诉你有文件被修改过,用git diff可以查看修改内容。
	git status
	git diff
提交后的历史查看
  1. 版本回退
  • HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset --hard commit_id。

  • 穿梭前,用git log可以查看提交历史,以便确定要回退到哪个版本。 加上--pretty=oneline参数:历史信息更简洁

  • 要重返未来,用git reflog查看命令历史,以便确定要回到未来的哪个版本。

	git reset --hard commit_id
	git reset --hard HEAD^
	git log
	git log --pretty=oneline
	git reflog
  1. 解决冲突
  • 用git log --graph命令可以看到分支合并图。
分支相关操作
  • 查看分支:git branch

  • 创建分支:git branch < name>

  • 切换分支:git checkout < name>或者git switch < name>

  • 创建+切换分支:git checkout -b < name>或者git switch -c < name>

  • 合并某分支到当前分支:git merge < name>

  • 删除分支:git branch -d < name>

  • 强制删除分支: git branch -D < name>

	git branch 
	git branch < name>
	git checkout < name>
	git switch < name>
	git checkout -b < name>
	git switch -c < name>
	git merge
	git branch -d < name>
	git branch -D < name>
撤销修改

命令git checkout -- file意思就是,把文件在工作区的修改全部撤销,这里有两种情况:

  • 一种是文件自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;

  • 一种是文件已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。

  • 总之,就是让这个文件回到最近一次git commit或git add时的状态。

  • 命令git reset HEAD < file>可以把暂存区的修改撤销掉(unstage),重新放回工作区

    git checkout -- file

总结:1、直接丢弃工作区的修改时,用命令git checkout -- file。 2、添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD < file>,就回到了1,第二步按1操作,用命令git checkout -- file。

远程仓库
  1. 从远程克隆
  • 要克隆一个仓库,首先必须知道仓库的地址,然后使用git clone命令克隆。

  • Git支持多种协议,包括https,但通过ssh支持的原生git协议速度最快。

    git clone

  1. 关联远程仓库
  • 要关联一个远程库,使用命令git remote add origin git@server-name:path/repo-name.git;

  • 关联后,使用命令git push -u origin master第一次推送master分支的所有内容;

  • 此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改;

	git remote add origin
	git push

3.从远处分支上拉取代码

  • git pull命令的作用是:取回远程主机某个分支的更新,再与本地的指定分支合并
  • git fetch命令的作用是:从远程获取最新版本到本地,不会自动merge
	git pull
	git fetch
标签管理
  1. 创建标签
  • 命令git tag < tagname>用于新建一个标签,默认为HEAD,也可以指定一个commit id;

  • 命令git tag -a < tagname> -m "blablabla..."可以指定标签信息;

  • 命令git tag可以查看所有标签。

  • 用命令git show < tagname>可以看到说明文字:

	git tag < tagname>
	git tag
	git show < tagname>
  1. 删除标签
  • 命令git push origin < tagname>可以推送一个本地标签;

  • 命令git push origin --tags可以推送全部未推送过的本地标签;

  • 命令git tag -d < tagname>可以删除一个本地标签;

  • 命令git push origin :refs/tags/< tagname>可以删除一个远程标签。

	git push origin < tagname>
	git push origin --tags
	git tag -d < tagname>
	git push origin :refs/tags/< tagname>
分支管理策略
  1. 分支策略 在实际开发中,我们应该按照几个基本原则进行分支管理: 首先,master分支应该是非常稳定的,也就是仅用来发布新版本,平时不能在上面干活; 那在哪干活呢?干活都在dev分支上,也就是说,dev分支是不稳定的,到某个时候,比如1.0版本发布时, 再把dev分支合并到master上,在master分支发布1.0版本;你和你的小伙伴们每个人都在dev分支上干活, 每个人都有自己的分支,时不时地往dev分支上合并就可以了 合并分支时,加上--no-ff参数就可以用普通模式合并, 合并后的历史有分支,能看出来曾经做过合并,而fast forward合并就看不出来曾经做过合并。

  2. BUG分支

    • 修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;

    • 当手头工作没有完成时,先把工作现场git stash一下,然后去修复bug,修复后,再git stash pop,回到工作现场; 一是用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;

    • 在master分支上修复的bug,想要合并到当前dev分支,可以用git cherry-pick < commit>命令,把bug提交的修改“复制”到当前分支,避免重复劳动。

  3. Feature分支

    • 开发一个新feature,最好新建一个分支;

    • 如果要丢弃一个没有被合并过的分支,可以通过git branch -D 强行删除。

  4. 远程分支

    因此,多人协作的工作模式通常是这样:首先,可以试图用git push origin < branch-name>推送自己的修改;

    如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;如果合并有冲突,则解决冲突,并在本地提交;

    没有冲突或者解决掉冲突后,再用git push origin < branch-name>推送就能成功!

    如果git pull提示no tracking information,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream-to < branch-name> origin/< branch-name>。

    这就是多人协作的工作模式,一旦熟悉了,就非常简单。

    总结:

    查看远程库信息,使用git remote -v;

    本地新建的分支如果不推送到远程,对其他人就是不可见的;

    从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交;

    在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称最好一致;

    建立本地分支和远程分支的关联,使用git branch --set-upstream branch-name origin/branch-name;

    从远程抓取分支,使用git pull,如果有冲突,要先处理冲突。