个人博客地址:https://deepzz.com。
git init 创建新仓库
$ cd test
$ git init
Initialized empty
Git repository
in /
Users/chen/test/.git/
git clone 克隆仓库
$ git clone git
@github.
com:deepzz
0/test.git [指定目录]
$ git clone /
Users/chen/test.git
git add 添加文件索引
$ git
add README
.md data/README2
.md #添加文件到索引,添加多个文件用空格隔开。
$ git
add .
#添加所有文件到索引,包含:修改的文件,新加的文件
git rm 删除文件或目录
$ git rm test.txt
$ git rm -rf data
$ git rm --cached test.txt
git reset 取消操作回退
$ git reset --hard
HEAD~
1
$ git reset --soft
HEAD ~
1
$ git reset --soft
HEAD^
git remote 远程仓库
cd test
#进入 test 文件夹
git init
#初始化仓库
$ git remote
add origin git@github
.com:deepzz0/test
.git #在本地添加远程仓库
$ echo “
# test” >> README.md #创建并写入 “# test” 到文件 README.md
$ git
add README
.md #添加 README.md 到版本控制下, 全部添加:$ git add .
$ git commit -m ‘first commit’
#提交时填写 message
$ git
push -u origin master
#首次提交,指定分支的upstream,此后在该分支pull/push都会关联到master
…..
To git@github
.com:deepzz0/test
.git
* [new branch] master -> master
…..
----------------------------------------------------------------------------------------------------------------
$ git remote
set-url --
add origin <url2>
#为本地仓库添加第二个远程仓库,这样一次提交可提交到所有 远程仓库。该操作会在 .git/config 下的 config 文件里增加一行。
$ git config -e
#查看 config 文件内容
----------------------------------------------------------------------------------------------------------------
$ git remote
set-url origin git@github
.com:deepzz0/test2
.git #你可以重新关联本地仓库到新的远程仓库地址。
git mv 重命名文件或目录
$ git
add testt
.txt #将 text.txt 添加到索引
$ git mv test
.txt newtext
.txt #将testgit
相当于:
$ mv test
.txt newtest
.txt #重命名
$ git rm --cached test
.txt #移除 test.txt 索引
$ git
add newest
.txt #添加到索引
git log 显示提交记录
$ git log
git status 查看文件状态
$ git status #查看当前仓库文件状态:新加文件未 git
add, 新加文件 git
add 过,新修改文件未 git
add.
git branch 分支
$ git branch
$ git branch -r
$ git branch -a
$ git branch deepzz
$ git branch -m oldbranch newbranch
$ git branch -d branchname
$ git branch -d -r branchname
git checkout 切换分支
$ git checkout branchname
$ git checkout tagname
$ git checkout master filename
$ git checkout commit_id filename
$ git checkout .
git diff 查看修改
$ git diff
$ git diff --cached
$ git diff --staged
$ git diff
HEAD
$ git diff branchname maste
$ git diff branchname
$ git diff
HEAD^
HEAD
$ git diff
SHA1 SHA2
git commit 提交修改
$ git commit -m “描述信息”
$ git commit -a -m “描述内容”
$ git commit -amend
git tag TAG相关
$ git tag
$ git tag -l ‘v
0.
1.*’
$ git tag v
0.
1.2-light
$ git tag -a v
0.
1.2 -m “
0.
1.2版本”
$ git checkout tagname
$ git tag -d v
0.
1.2
$ git push origin
:v0.
1.2
$ git tag -a v
0.
1.1 9fvc3d1
$ git push origin v
0.
1.2
$ git push origin -tags
git merge 分支合并
$ git checkout deepzz
$ git merge master
$
$ git merge —squash master
$ git commit -m ‘merge from master’
$
$ git cherry-pick
321d76f
$ git cherry-pick -n
321d76f
324dcj5
$
<<<<<<<
HEAD
test
in master
=======
test
in deeps
>>>>>>> deepzz
说明:
<<<<<<<当前分支内容开始
======当前分支结束,以后是merge过来的代码
>>>>>>>
git rebase 查看修改
$ git checkout deepzz
$ git rebase master
原理:
rebase时,会将deepzz所有的提交(commit)取消掉,并且把它们保存为补丁(patch)(保存在 .git/rebase 目录中)。
之后,将最新的 master 分支更新到当前分支(相当于 merge 了一份最新代码到一个新的分支)。然后,再将这些补丁应用到当前分支,当然这可能会有冲突。
git fetch 取回本地
$ git fetch origin master
git stash 保存工作现场
$ git stash list
$ git stash
$ git stash save -a “message”
$ git stash pop
$ git stash pop stash@{num}
$ git stash clear
$ git stash drop
git pull 获取代码
$ git pull <远程主机名> <远程分支名>
:<本地分支名>
$ git pull origin
master:deepzz
$ git pull origin master
$ git pull —rebase <远程主机名> <远程分支名>
:<本地分支名>
git push 提交代码
$ git push <远程主机名> <本地分支名>
:<远程分支名>
$ git push origin master
$ git push origin
:master
$ git push -u origin master
$ git push —all origin
问答
git fork后的项目如何更新? 现在有项目A,其地址是A_REPOSITORY_URL。fork到自己仓库B,其地址B_REPOSITORY_URL。现在A进行了commit更新,那么B如何同步更新呢?
$ git clone
B_REPOSITORY_URL
$ git remote add upstream
A_REPOSITORY_URL
$ git pull upstream master
$ git push origin master
> 摘自知乎:
https:/
/www.zhihu.com/question/20171506/answer/15674190
示意图
转载请注明原文地址: https://ju.6miu.com/read-17413.html