A branch management
Cd # / the Data / gitroot # git Branch // view the current warehouse branch * master // here only one line, where the number of the current branch * Which # git branch aming // create a branch, called Aming # git Branch Aming * Master
// * number is still master, note also maser branch on
# Git checkout aming // git checkout, switching branch switch to branch 'aming' # // View Branch git branch * the number has reached aming * aming master # LS // current branch and in the master file is the same as 1 .txt # vim 2.txt // write a new file in aming branch # git the Add. # git commit -m "the Add 2.txt" // got to make changes to the repository, you only need git commit it. [gitroot the MRX the root @] LS # 1.txt 2.txt [the root @ gitroot the MRX] # Git Checkout master switch to branch 'master' [the root @ gitroot the MRX] LS # // switch to the master view is not 2.txt the 1.txt
Branches are isolated from each other, the operation of the file between the branch and the branch is not affected mutually.
Branches merge
aming branch made changes, master branch did not do, and now want to change the two branches of the same, can merge branches. Aming branch into the master branch up, these two branches would remain in agreement.
# Git checkout master // before merging branches, switch to the target branch # git merge aming // aming branch in the changes incorporated into the master and come.
If the master branch and branches are aming to 2.txt edited, when the merger would prompt conflict, need to resolve those conflicts before continuing merger.
The solution of the conflict is in the master branch, edit 2.txt, instead of the content aming branch inside 2.txt. Then submit 2.txt, then merge aming branch.
But there is such a problem, in case the contents of the master branch that we want to change it? 2.txt can edit the content, you want to change, and then submit. Aming switch to branch to the master branch and merge aming to the branch (merge backwards). Merge branch there is a principle that should be incorporated into the latest branch old branch. That later merge with the branch name must be the new branch. Grasp this principle, you can not go wrong.
Git Branch # Aming * master [root @ MRX gitroot] # vim 2.txt // 2.txt under the master branch adds a few lines uipad uviadp euqpvan 4,989,496 hgiasd iahusdf # git 2.txt the Add # git the commit -m "CH 2 .txt " # git Checkout Aming at # vim 2.txt // aming branch 2.txt delete a few lines uipad uviadp euqpvan # git the Add 2.txt # git the commit -m" CH 2.txt " # git Checkout Master # git merge aming automatically merge 2.txt conflict (content): merge conflict in 2.txt automatically merge fails, then submit an amended correction conflict results. # Cat 2.txt // 2.txt in different parts of the content at this time will be automatically marked. uipad uviadp euqpvan <<<<<<< the HEAD hgiasd iahusdf ======= >>>>>>> Aming [root @ MRX gitroot] # vim 2.txt uipad uviadp euqpvan # git Checkout Aming 2.txt: Needs Merge error: You need to be resolved before the current index conflict // yet another problem here, when you merge conflict, you can not switch to another branch down. So try to avoid merge conflicts # git the Add 2.txt # git the commit -m "CH 2.txt" # git Checkout Aming switch to the branch 'Aming' # git Aming // Merge the two branches of the content is consistent, and again on merger It does not make any difference. Already up-to-date.
# Git branch -d aming // delete branches. If you are under the branch, you can not delete the current the current branch, you can switch to another branch.
If the branch is not merging, you will be prompted before deleting it would not merge, forced to delete:
# Git branch -D our
Second, the remote branch management
Use doctrine branches
For applications branch, it is recommended to this principle:
master branch is very important line of code with the release of this branch, we usually do not develop code on the branch.
Create a dev branch, specifically as a development only if prior to publishing online, will be merged into the master branch dev
Developers should then branches into individual branches, individual branch (on your own pc) which developed the code, and then merged into the dev branch on the basis dev.
dev branches merge bob branch command is:
# Git checkout dev // switch to the dev branch first, and then # git merge bob
Remote branch
The new local branch if you do not push the remote, that is not visible to others.
Common sense: git clone can clone a master branch.
Create a new file linux.doc and a new branch dev.
After you create a successful branch, click again, you can see the check mark in front of the dev, where you can choose to replace the branches.
View branch back to the command line, the first clone of this project can be found only a master branch.
View remote branch git ls-remote origin, you can see all the branches.
# git ls-remote origin 45337b0d65a36760796a0f3a45e3f90e1d38ed4e HEAD 41b9784a6f7aeecb1d485e956b10232b5114592a refs/heads/dev 45337b0d65a36760796a0f3a45e3f90e1d38ed4e refs/heads/master
For branch git push two cases
When the same local branch and a remote branch git push will change all local branches together pushed to the remote, such as master and dev.
If you want to push only one branch, using git push origin branch-name.
When the local branch more than the remote branch, only the default git push to push local and remote branches of the same, when you want that extra push to the local branch of the remote, use git push origin branch-name If push failed to grab a git pull the new remote submission.
git clone的时候默认只把master分支克隆下来,如果想把所有分支都克隆下来,需要手动创建,在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称要一致。这里的两个branch-name指远程分支的名字。
# git checkout -b dev origin/dev 分支 dev 设置为跟踪来自 origin 的远程分支 dev。 切换到一个新分支 'dev' # vim 4.txt # git add 4.txt # git commit -m "add 4.txt" # git push 0b51fcb..43fd64d dev -> dev //git push后,最后一行可以看到从dev推送到了dev。 再到远程上刷新,可以看到dev分支下出现了4.txt,而master是没有的。
三、标签管理
标签类似于快照功能,可以给版本库打一个标签,记录某个时刻库的状态。也可以随时恢复到该状态。
git checkout master 先切换到master分支上,以后打标签也是针对master分支来做。 git tag v1.0 给master打一个标签v1.0 git show v1.0 查看标签信息 git tag 查看当前分支下所有的标签 tag是针对commit来打标签的,所以可以针对历史的commit来打tag,v1.0是对最新的版本打的标签。 git log --pretty=oneline --abbrev-commit //先查看历史的commit,这里后面的选项指用简写的commit。 git tag v0.9 46d3c1a //针对历史commit打标签 git tag -a v0.1 -m "first tag" 2ec587f //可以对标签进行描述
# git show v0.1 tag v0.1 Tagger: aming Date: Thu Sep 19 11:06:29 2019 +0800 first tag //查看标签信息可以看到描述 commit 2ec587ff6e9e0e4c67aefb84e02b2b73693d633e Author: aming Date: Sun Sep 8 14:59:15 2019 +0800 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..6f0cbde --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# apelearn
git tag -d v0.8 //删除标签
前面的操作都是在客户端,还没有推送到远程服务端。
查看远程上的tags方法,在切换分支的选项里:
git push origin v1.0 //推送指定标签到远程
git push --tag origin //推送所有标签
如果本地删除了一个标签,远程也想要删除需要这样操作:
git tag v0.8 -d //删除本地标签
git push origin :refs/tags/v0.8 //删除远程标签
四、git别名
git commit 这个命令可以用别名表示,用别名可以提高我们的工作效率。 格式: git config --global alias.别名 命令名 git config --global alias.ci commit git config --global alias.co checkout git config --global alias.br branch 这些别名的配置都在/root/.gitconfig文件中,可以在文件内进行更改。 查看git别名使用命令 git config --list |grep alias 查询log小技巧: # git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" # git lg //可以看到用户,时间,描述和tag。 * 45337b0 - (HEAD, tag: v1.0, origin/master, origin/HEAD, master) add 3.txt (6 天之前) * 41b9784 - Create linux.doc (7 天之前) * d816b47 - add 1.txt (13 天之前) * 0c8b170 - add 2.txt (13 天之前) * 2ec587f - first commit (13 days ago) cancel the alias git config --global --unset alias.br
to sum up:
Branch Management
View branch git branch
git checkout master branch switching
git merge aming aming branch in the changes to merge into master
git branch -d aming deleted branches
git branch -D aming forced to remove branches
Remote Branch Management
git ls-remote origin view remote branch
git push origin branch-name Push single branch
Creates a local branch and a remote branch corresponds, using git checkout -b branch-name origin / branch-name
Label Management
git tag v1.0 to v1.0 master branch playing a label
git show v1.0 view label information
git tag View all tags
git log --pretty = oneline --abbrev-commit view the history of commit
git tag -a v1.0 -m "first tag" 2ec587f description tag to do
git tag -d v0.8 delete label
git push origin v1.0 push the specified label to a remote
git push --tag origin push all tags
git tag v0.8 -d delete the local label
git push origin: refs / tags / v0.8 delete remote tag
git aliases
git config --global alias. alias command name
git config --list | grep alias View alias
git config --global --unset alias.br