1. Install Git
Official website installation guide address
Use HomeBrew to install under MAC
If HomeBrew is not available, try installing it by typing the following command in the terminal:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Use brew to install the version of Git
$ brew install git
2. Git related commands
Order | Related operations | explain |
---|---|---|
init | $ git init | Create a local repository |
config | $ git config –global user.name “yourName” | Configure name information |
$ git config –global user.email “yourEmail” | Configure email information | |
add | $ git add fileName | Add files to staged state |
$ git add . | Add all files to staged state | |
status | $ git status | View the current status of library files |
commit | git commit -m "fill in the relevant description information" | Commit the staged file to the local repository |
$ git commit -am "fill in the relevant description information" | Merge the add operation in one step | |
$ git commit –amend –no-edit | Append the operation that has been committed. At this time, the last record will be deleted and a new record will be generated (the commit id is different). Note that the changed file must be added to the staged state first. | |
log | $ git log | View the operation log of the library |
$ git log –oneline | View the key information of the operation of the library, each commit content is displayed on one line | |
$ git log –oneline –graph | View key information about the operation of the library, including branch information | |
diff | $ git diff | View files in unstaged state |
$ git diff –cached | View files in staged status (added files) | |
$ git diff HEAD | 查看 staged & unstaged (HEAD) | |
reset | $ git reset fileName | Return to the state before add (unstaged) |
$ git reset –hard HEAD | View the id pointed to by the HEAD pointer of the last commit | |
$ git reset –hard commitId | Before going back to commit (rollback): hard will clear all operation commits after 7cd4e4e | |
$ git reset –soft commitId | Back before commit (rollback): soft will keep all operations after 7cd4e4e | |
reflog | $ git reflog | View all HEAD changes, with reset –hard to restore the previous delete operations |
checkout | $ git checkout dev1.0 | switch branch/tag |
$ git checkout -b dev1.0 | Create and switch local branches | |
$ git checkout -b dev1.1 origin/dev1.1 | add remote branch to local | |
$ git checkout commitId – fileName | Roll back the file to a commit | |
branch | $ git branch | View current branch |
$ git branch -a | View all branches | |
$ git branch dev1.0 | create branch | |
merge | $ git works dev1.0 | merge branch |
$ git branch -d dev1.1 | delete local branch | |
$ git merge -no-ff -m "Related description information" dev1.0 | Merge the branch, keep the commit information on the branch | |
stash | $ git stash | Staging changes |
$ git stash list | View staging tasks in stash | |
$ git stash pop | Continue staging tasks | |
fetch | $ git fetch origin dev1.0 | Pull the remote repository to the local repository (need to merge again) |
pull | $ git pull origin dev1.0 | Pull the remote repository to the local repository and merge it into the local project |
push | $ git push -u origin dev1.0 | Push the local repository to the remote repository |
$ git push origin :branch-name | delete remote branch or tag | |
$ git push origin branch-name1:branch-name2 | replace remote branch | |
remote | $ git remote add origin remoteAddress | 添加并链接到远程库 |
$ git remote rm origin | 删除远程库 | |
tag | $ git tag | 查看标签 |
$ git tag v1.0.0 -light | 创建轻量标签 | |
$ git tag -a v1.0.0 -m “release 0.1.0 version” | -a即annotated的缩写,指定标签类型,后附标签名 | |
$ git push origin v1.0.0 | 发布标签,提交到git服务器 | |
$ git push origin –tags | 将本地所有标签一次性提交到git服务器 | |
$ git tag -d v1.0.0 | 删除标签 | |
$ git checkout tagname | 切换标签 | |
$ git tag -a v1.0.0 commitId | 补打标签 |
init
进入相关文件夹创建本地版本库
$ git init
config
配置相关信息
$ git config --global user.name "yourName
$ git config --global user.email "yourEmail"
add
添加文件到staged状态
$ git add <file>
添加所有文件到staged状态
$ git add .
status
查看库文件当前状态
$ git status
commit
将处于stage状态的文件提交到本地库中
$ git commit -m "填写相关描述信息"
合并add操作,一步到位
$ git commit -am "填写相关描述信息"
追加已经commit的操作, 此时会删除最后一条记录并产生新的一条记录 (commit id 不同),注意要先将改动的文件add到staged状态
$ git commit --amend --no-edit
log
查看库的操作日志
$ git log
查看库的操作关键信息,每个 commit 内容显示在一行
$ git log --oneline
查看库的操作关键信息,包括分支信息
$ git log --oneline --graph
diff
查看unstaged状态的文件
$ git diff
查看 staged状态的文件(add过的文件)
$ git diff --cached
查看 staged & unstaged (HEAD)
$ git diff HEAD
reset
回到add之前状态(unstaged)
$ git reset <file>
查看上次 commit 的HEAD指针指向的id
$ git reset --hard HEAD
回到 commit 之前(回滚): hard 会清除 7cd4e4e 之后的所有操作提交
$ git reset --hard <commit id>
回到 commit 之前(回滚): soft 会保留 7cd4e4e 之后的所有操作
$ git reset --soft <commit id>
reflog
查看所有 HEAD 的改动,配合 reset –hard 的可恢复之前的删除操作
$ git reflog
checkout
让文件回滚到某一次 commit
$ git checkout <commit id> -- <file>
切换分支
$ git checkout dev1.0
创建并切换分支
$ git checkout -b dev1.0
branch
查看当前分支
$ git branch
$ git branch -a #查看全部分支
创建分支
$ git branch dev1.0
merge
合并分支
$ git merge dev1.0
合并分支,保留分支上commit信息
$ git merge --no-ff -m "相关描述信息" dev1.0
stash
暂存修改
$ git stash
查看 stash 中的暂存任务
$ git stash list
继续暂存任务
$ git stash pop
fetch
拉取远程版本库到本地版本库(需要再次merge)
$ git fetch origin dev1.0
pull
拉取远程版本库到本地版本库,并将其merge到本地项目中
$ git pull origin dev1.0
push
推送本地版本库到远程版本库
$ git push -u origin dev1.0
三、常见问题
解决merge冲突
<<<<<<< HEAD
# edited in master
=======
# edited in dev
>>>>>>> dev
删除”<<<<”,”====”,”>>>>”中问题code,重新提交
$ git commit -am "解决xxx冲突"
链接本地版本库,并将分支 push 推送到Git上
$ git remote add origin git@github.com:LOLITA0164/PhotoBrowser.git
$ git push -u origin master # 推送本地 master 去 origin
$ git push -u origin dev # 推送本地 dev 去 origin
注意事项:
正确姿势出现如下图样式
出现fatal: remote origin already exists.错误
如果链接本地版本库出现上面错误,尝试删除重试
$ git remote rm origin
pull时候出现fatal: refusing to merge unrelated histories
因为网上和本地的是不同的两个仓库,(网上的已经初始化过了,这种情况通常是clone下来再开发)如果需要合并,输入以下命令尝试
git pull origin <分支如master> --allow-unrelated-histories
Clone 克隆Git上的版本库
git clone sourceAddress destinationAddress
如:
git clone git@github.com:LOLITA0164/PhotoBrowser.git ~/Desktop/PhotoBrowser
添加远程分支到本地分支
$ git checkout -b dev1.1 origin/dev1.1
删除本地分支
$ git branch -d dev1.1
四、过程
…
1、拉取最新需要开发的分支到本地(fetch、merge)
2、开发功能,push前,再次拉取最新code
3、1-2循环
4、版本完成,merge到master分支,将该版本push到远程库中
5、打上tag,release该版本
6、1-5循环