Git本身就是一个代码版本控制器。针对一个项目开发过程,我们需要不断的添加新的功能或者修改bug,也就是要对代码的版本不断的备份和迭代。如何方便的对之前的代码进行备份,或者将当前的修改和之前的代码进行对比。这就促使了代码版本控制器的产生。
此外,在进行代码开发过程中,可能涉及到不同的开发人员对同一个项目进行开发,这就需要保证不同的开发人员开发的代码互不干扰,但又能把每个人开发出的功能合并到主代码库中。这也是git具备的一个重要功能。
Git最主要是理解一些区的相关概念,即:

(1) 克隆某个代码库并切出来自己的分支
// 比如clone freeswitch
git clone https://github.com/signalwire/freeswitch.git
//基于当前分支切出新的分支,这里的my_branch就是新分支的名字
git checkout -b my_branch
// 关联到远程仓库,唯有关联后才能把自己修改的代码push到远程库
git push --set-upstream origin my_branch
// 查看关联是否成功
git branch -vv
远端的代码可能有多个分支,因此也可以切到其他已有的分支
// 查看远程库中所有分支版本
git branch --all
// 切到已有分支A-branch
git checkout A-branch
(2) 个人修改代码提交
git add.
git commit -m"本次修改的注释"
git push
// 可以使用diff查看当前修改和其他分支的不同
git diff my_branch other_branch
(3) 将个人分支(my_branch)做的修改合并到master分支
// 切换到master分支
git checkout master
// 更新master分支
git pull
// 将my_branch上的修改合并到master
git git merge my_branch
(4) git查看历史的代码提交记录
https://www.runoob.com/git/git-commit-history.html
git log - 查看历史提交记录
得到如下内容
commit c68142b562c260c3071754623b08e2657b4c6d5b
Author: runoob <test@runoob.com>
Date: Fri May 3 15:52:12 2019 +0800
修改代码
commit 777424832e714cf65d3be79b50a4717aea51ab69 (change_site)
Author: runoob <test@runoob.com>
Date: Fri May 3 15:49:26 2019 +0800
changed the runoob.php
其中 commit 后的字符串(c68142b562c260c3071754623b08e2657b4c6d5b)就是版本号。
(5) git版本回退
git log
git reset --hard 版本号
(4) 将某个分支的所有修改合并到master
首先切换到master分支
git merge dev-name
(5) git版本回退
git log
git reset --hard 版本号
(6) 远程分支的强制回滚(一般不用)
git checkout current_branch_name # 切换到当前分支
git reset --hard commit_id # 回滚到某次提交
git push -f origin current_branch_name # 强制回滚