Mac 用户家目录下 .gitconfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
git config --gloabl user.name "VaneHay"            ## 全局
git config --global user.email "VaneHay@163.com" ## 全局

cd project ## project 项目
git config user.name "Test" ## 项目
git config user.email "VaneHay@163.com" ## 项目

git config --global alias.st status ##设置别名 git st = git status

//查
git config --global --list
git config --global user.name

//增
git config --global --add user.name TestName

//删
git config --global --unset user.name

//改
git config --global user.name TestName

创建并初始化

1
2
mkdir test
git init

创建分支

1
git branch abc

查看分支

1
git branch -al

重命名分支

1
2
3
4
git branch -m oldName newName 
git push origin newName //推送分支到远程
git push --delete origin oldName //删除远程旧分支 或 git push origin :branchName
git branch -d branchName //在本地删除一个分支

恢复已删除的分支

1
2
git log -g   //查看提交的 commit
git branch new_branch[新分支] commit_id //用这个commit创建一个分支

reset 与的区别

1
2
3
4
5
6
7
8
9
10
git reset 的作用是修改HEAD的位置,目标版本之后提交的版本会不见
操作
git log 查看 目标版本的 commit_id
git reset --hard 目标版commit_id
git push -f

git revert是用于“反做”某一个版本,以达到撤销该版本的修改的目的。比如,我们commit了三个版本(版本一、版本二、 版本三),突然发现版本二不行(如:有bug),想要撤销版本二,但又不想影响撤销版本三的提交,就可以用 git revert 命令来反做版本二,生成新的版本四,这个版本四里会保留版本三的东西,但撤销了版本二的东西。
git log 查看 反做 的版本 commit_id
git revert -n 版本 commit_id //使用“git revert -n 版本号”反做,并使用“git commit -m 版本名”提交:
git push

合并

1
2
3
4
5
git checkout master       //切换master分支
git checkout --patch dev filename.txt //合并 dev 分支上的 filename.txt 到master分支上

如果 master 分支没有 filename.txt 文件的话
git checkout dev filename.txt 把

例子:使用命令行创建仓库

1
2
3
4
5
6
git clone git@e.coding.net:vanehay/test/test.git
cd test
echo "# test" >> README.md
git add README.md
git commit -m "first commit"
git push -u origin master

例子:使用命令行推送已存在的仓库

1
2
git remote add origin git@e.coding.net:sxydkj/test/test.git
git push -u origin master

例子:更换远程仓库地址方法

1
2
3
4
5
6
7
8
9
10
11
1. 通过命令直接修改远程仓库地址
git remote 查看所有远程仓库
git remote xxx 查看指定远程仓库地址
git remote set-url origin 你新的远程仓库地址

2. 先删除后添加
git remote rm origin 删除远程仓库地址
git remote add origin 你的新远程仓库地址

3. 修改 `.git` 文件
.git文件中的config文件修改config文件中的url路径为你的新远程仓库地址路径。

例子:分之的创建、删除、切换、合并

1
2
3
4
5
git branch -a 查看所有分支 
git checkout -b dev 新建 dev 分支 相当于: git branch dev 与 git checkout dev 2条命
git branch -d dev 删除分支
git checkout dev 从当前的master分支切换到dev分支上面
git merge dev 命令用于合并指定分支到当前分支

例子:使用回退的方式解决 error: Your local changes to the following files would be overwritten by merge: 问题

1
2
3
4
5
6
7
8
9
10
11
1.进入到文件所在文件目录,或者能找到文件的路径
git log test.php

2.回退到指定的版本
git reset a4e215234aa4927c85693dca7b68e9976948a35e test.php

3.提交到本地参考,注意不需要git add
git commit -m "revert old file because yjl commmit have a bug"

4.更新到工作目录
git checkout test.php

例子:git pull 单一文件

1
2
3
git fetch origin master
git checkout origin/master c.txt
git checkout origin/master -- path/to/file
参考:https://www.liaoxuefeng.com/wiki/896043488029600/900003767775424