git usage:常用git命令

最近在改TV media相关的测试用例,需要在git上维护相关的脚本,把常用命令总结如下,方便以后使用.

1. 从已有git服务器上clone到本地,

首先进入用户根目录: cd ~

然后进行clone: git clone littledrop@IP:/automation my-master

注: 这里littlerop@IP:/automation是git server的url, my-master指的是本地rep的名称,可以省略.如果省略,则自动用url中的automation.

执行上述命令后,在本地自动创建了master分支,路径位于~/automation

2. 然后手工创建自己的分支,并立刻切到此分支

git branch -b littledrop

这个分支与master分支内容一样

3. 通过git branch -a可以查看当前所有分支(包括本地及远程),标*的为当前所工作的分支

4. 在branch littledrop上开始修改代码file.txt,修改完后决定提交,用以下命令

git add file.txt

git commit -m "这里输入提交时候的comment"

5. 在remote上新建一个跟littledrop对应的远程分支

git push origin littledrop:littledrop

注: origin代表的是远端的repository, 前一个littledrop表示本地branch<src>, 后一个littledrop代表远端branch<dst>, 如果远端没有名为littledrop的分支,则自动创建.  如果省略了<dst>,git就认为你想push到remote repository下和local branch相同名字的branch。

6. 通过git log可以看到当前分支上的历史信息

7. 如果对刚刚的commit后悔了,需要撤掉,用

git revert HEAD(撤销前一次的commit)

或者是

git revert commit-id

这里commit-id可以通过git log看到.(比如:fa042ce57ebbe5bb9c8db709f719cec2c58ee7ff)撤销指定的版本,撤销也会作为一次提交进行保存。
git revert是提交一个新的版本,将需要revert的版本的内容再反向修改回去,版本会递增,不影响之前提交的内容

注: git revert的动作通过git log能够看到,如果希望在git log里也去掉刚刚commit的记录,用git reset

8. 用git reset撤销刚刚的commit,这样清除后通过git log就看不到这次撤销的记录

git reset --hard commit-id

9. 用git config配置用户名和email, 即提交时显示的作者信息,

git config --global user.name "littledrop"

git config --global user.emal littledrop@xxx.com

git commit --amend --reset-author

10.对不同的branch进行merge,如把master上的内容merge到littledrop分支上

先checkout到littledrop: git checkout littledrop

再进行merge:  git merge master

11. 如果远程master有更新,本地也要及时pull下来

git pull origin master

12. 通过git blame可以追踪每个文件的各行代码是由谁修改的

git blame file.txt

原文地址:https://www.cnblogs.com/littledrop/p/3654745.html