参考廖雪峰网站
初始化一个Git仓库,使用
git init
命令。
添加文件到Git仓库,分两步:
-
第一步,使用命令
git add <file>
,注意,可反复多次使用,添加多个文件; -
第二步,使用命令
git commit
,完成。
要随时掌握工作区的状态,使用
git status
命令。
如果
git status
告诉你有文件被修改过,用
git diff
可以查看修改内容。
版本回退
-
HEAD
指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset --hard 版本号
。 -
穿梭前,用
git log 或者 git log --pretty=oneline (
--abbrev-commit
)
可以查看提交历史,以便确定要回退到哪个版本。 -
要重返未来,用
git reflog
查看命令历史,以便确定要回到未来的哪个版本。
场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令
git checkout -- file
。
场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令
git reset HEAD file
,就回到了场景1,第二步按场景1操作。
场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考
版本回退一节,不过前提是没有推送到远程库。
删除文件
命令
git rm
用于删除一个文件。
Git 全局设置:
git config --global user.name "****" git config --global user.email "*****"
创建 git 仓库:
mkdir afafa
cd afafa
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://git.oschina.net/*/*.git
cd afafa
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://git.oschina.net/*/*.git
git push -u origin master
这个是ssh的路径
git remote set-url origin git@git.oschina.*/*.git
已有项目?
cd existing_git_repo
git remote add origin https://git.oschina.net/*/*.git
git push -u origin master
git remote add origin https://git.oschina.net/*/*.git
git push -u origin master
要关联一个远程库,使用命令
git remote add origin git@server-name:path/repo-name.git
;
关联后,使用命令
git push -u origin master
第一次推送master分支的所有内容;
推送到服务器
此后,每次本地提交后,只要有必要,就可以使用命令
git push origin master
推送最新修改;
克隆项目
要克隆一个仓库,首先必须知道仓库的地址,然后使用
git clone
命令克隆。
git clone git@git.oschina.*/*.git
Git支持多种协议,包括
https
,但通过
ssh
支持的原生
git
协议速度最快。
分支
查看分支:
git branch
git branch -a 列出服务器所有分支
创建分支:
git branch <name>
切换分支:
git checkout <name>
创建+切换分支:
git checkout -b <name>
删除远程分支:git push origin :branch_name
合并某分支到当前分支:
git merge <name>
不使用fast forward模式 :git merge --no-ff -m "merge with no-ff" dev
合并分支时,加上
--no-ff
参数就可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而
fast forward
合并就看不出来曾经做过合并。
删除分支:
git branch -d <name>
从git上拉取别人的分支:
mkdir tsfile
cd tsfile
git init
git remote add origin git@git.oschina.net:*/*.git
git fetch origin branch_name
git pull origin branch_name
从git上clone项目并拉取分支:
git clone git@git.oschina.net:*/*.git
cd **
git branch <branch_name>
git checkout <branch_name>
git fetch
git pull origin <branch_name>
拉取别人分支:
git checkout -b branchname origin/branchname
查看分支合并图
git log --graph --pretty=oneline --abbrev-commit
保存分支现场
git stash
查看保存的分支
git stash list
将分支恢复并删除
git stash pop
多次stash,恢复的时候,先用
git stash list
查看,然后恢复指定的stash
git stash apply stash@{0}
删除没有合并过的分支
git branch -D <name>
强行删除。
多人协作的工作模式通常是这样:
-
首先,可以试图用
git push origin branch-name
推送自己的修改; -
如果推送失败,则因为远程分支比你的本地更新,需要先用
git pull
试图合并; -
如果合并有冲突,则解决冲突,并在本地提交;
-
没有冲突或者解决掉冲突后,再用
git push origin branch-name
推送就能成功!
如果git pull
提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream 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
,如果有冲突,要先处理冲突。