git 使用流程和常用指令

git 使用流程和常用指令

Git的使用流程,首先配置Git环境,配置好Git账号;配置好远程仓库,得到仓库地址。
仓库内没有内容时,将本地项目Git初始化,配置远程仓库,第一次将代码强制提交到仓库内。
仓库内有内容时,直接从远程仓库git clone到本地。

1. 配置Git账号
# 使用码云
 git config --global user.name "Ting"
 git config --global user.email "32450@qq.com"
2. 本地项目Git初始化
//本地初始化
git init 
3. 给本地项目添加/删除/查看远程仓库
#添加远程仓库
git remote add origin https://gitee.com/MelissaHan/Headline.git

#删除远程仓库
git remote rm origin

#查看远程仓库
git remote -v
4. clone项目
git clone  https://gitee.com/MelissaHan/Headline.git
5. 第一次提交
#添加文件
git add . 
#本地提交
git commit -m "提交"
#提交到远程仓库
git push origin master
# -f:强制提交,不要之前的代码
git push origin master -f
6. 之后提交的操作
git add .
git commit -m "fix bug"
# 从远程仓库那拉取,然后解决冲突,之后再提交
git pull --rebase
git push origin master

7. 清空Git内容

如果我们上传了一个项目到git并已经commit和push了所有内容,但是忘记搞gitignore文件,
导致一些不想加入版本控制的文件,如IDE配置文件,编译文件,部署文件等,

# 首先配置好你的.gitignore文件,然后
#清空git内容
git rm -r --cache .
git add .
git commit -m "gitignore working"

8. git 回退操作

工作区,暂存区,本地仓库,远程仓库

8.1. 5种状态

未修改(Origin)
已修改(Modified)
已暂存(Staged)
已提交(Committed)
已推送(Pushed)

8.2. 检查修改
 #已修改,未暂存(检查工作区与暂存区间的差异)    
 git diff
 #已暂存,未提交(检查暂存区与本地仓库间的差异)
 git diff --cached
 #已提交,未推送(检查本地仓库与远程仓库间的修改)
 git diff master origin/master
8.3. 撤销修改
# 1.已修改,未暂存(撤销工作区的修改)
git reset --hard
# 2.已暂存,未提交(撤销暂存区的修改)
git reset --hard
# 3.已提交,未推送(撤销本地仓库的修改)
#origin/master为远程仓库,从远程仓库获取覆盖本地仓库代码
git reset --hard origin/master
# 4.已推送(撤销远程仓库的修改)
git reset --hard HEAD^
git push -f

## 此时本地仓库和远程仓库是一样的
## 1.第一步先恢复本地仓库
## 2.第二步再强制同步本地仓库到远程仓库

8.4 回滚到任一版本
# 显示提交的前三次的log
 git log -3
 /**
commit 970962ba72f7840b7996f394446784f3e42edc57 (HEAD -> master, origin/master)
Author: TingHan <3245003511@qq.com>
Date:   Wed Nov 28 14:27:48 2018 +0800

    fix 8

commit 861977e1252f9ad21785647d8d27e46387f0c0e5
Author: TingHan <3245003511@qq.com>
Date:   Wed Nov 28 14:27:17 2018 +0800

    fix 7
*/
# 得到hash值后,依据hash值回滚
 git reset --hard c2ea7d66aabd1f994749ae2f910b7826634f0772
 git push origin master -f

9. 分支操作
9.1 查看分支
git branch //查看本地分支
git branch -r //查看远端分支
git branch -a //查看所有分支
git branch -vv //查看本地分支状态,远端对应关系,落后或者优先多少 最后一次提交信息以及hash值
9.2 创建分支,切换分支,提交,远程推送分支
# 本地创建一个分支
git branch testingBranch
# 切换到testingBranch分支
git checkout testingBranch

# 在testingBranch分支,提交
git add .
git commit -m 'testingBranch fix 1'
# 切换到master分支查看代码,然后再切换回来
git checkout master

# 提交到远程仓库的testingBranch分支
git push origin testingBranch
#加了参数-u后,以后即可直接用git push 代替git push origin testingBranch
git push  -u origin testingBranch
# 下一次可以直接使用
git push

9.3 合并分支
# 首先切换到主分支,然后合并分支
git checkout master
git merge testingBranch

#合并分支,--no-ff:关闭Fast-Foward合并,这样可以生成merge提交,保留分支合并信息
git merge testingBranch --no-ff

#取消合并
git merge --abort 

9.4 解决合并分支的冲突
#1. 遇到冲突 
git merge testingBranch
Auto-merging GitDemo/ViewController.m
CONFLICT (content): Merge conflict in GitDemo/ViewController.m
Automatic merge failed; fix conflicts and then commit the result.

#2. 查看冲突详情
git status 

#3. 代码中解决冲突
#代码
    int a = 8,b = 9;
<<<<<<< HEAD
    int c = a + b;
=======
    int c = a * b;
>>>>>>> testingBranch
    printf("%d",c);
#4. 提交解决冲突后的代码
git add .
git commit -m 'merge conflict'

9.5 合并后删除分支,恢复分支
# 删除本地分支
git branch -d testingBranch

#恢复被删除的分支,首先获取到分支的散列值,然后根据散列值恢复分支
#获取git所有的commit
git reflog
eaa8e46 HEAD@{11}: commit: testingBranch fix 4
9050a7a HEAD@{12}: checkout: moving from master to testingBranch
9050a7a HEAD@{13}: merge testingBranch: Fast-forward
5a8a86f HEAD@{14}: checkout: moving from testingBranch to master
9050a7a HEAD@{15}: commit: testingBranch fix 3
c1e9e8f HEAD@{16}: commit: testingBranch fix 2
#拿到一个commitId,利用这个id创建这个分支,最好用最新的分支操作的commit。
#采用9050a7a这个commit,得到的分支是testingBranch fix 3这个提交版本
git branch testingBranch 9050a7a

# 删除远程分支
git push origin --delete testingBranch

9.6 远程合并分支

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

# 1
git fetch orgin master //将远程仓库的master分支下载到本地当前branch中
git log -p master  ..origin/master //比较本地的master分支和origin/master分支的差别
git merge origin/master //进行合并

# 2
git fetch origin master:tmp //从远程仓库master分支获取最新,在本地建立tmp分支
git diff tmp //將當前分支和tmp進行對比
git merge tmp //合并tmp分支到当前分支

#合并远端本地(不用先checkout到本地)
git fetch //先更新远程仓库所有分支上最新commit-id到本地
git merge origin/<branch> --no-ff //再从远端merge,无需先拉取本地

9.7 分支关联问题
#There is no tracking information for the current branch.
#Please specify which branch you want to rebase against.
#本地分支和远程分支没有建立联系
//查看分支
git branch -vv 
//* master c2ea7d6 [origin/master: ahead 1] fix 3
//关联本地分支和远程分支
git branch --set-upstream-to=origin/master master
//Branch 'master' set up to track remote branch 'master' from 'origin'.
10. 配置gitignore

GitHub下载文件:
https://github.com/github/gitignore

touch .gitignore //创建一个.gitignore文件
open .gitignore //打开,将GitHub的文件内容复制到这个文件中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值