In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Git Advanced (2)
=
Overview:
=
Git branch
1. Branch naming rules and common commands
Master (trunk branch) is the most recent commit to a specified branch
Dev is the most recent commit to the dev branch
No matter how many branches there are, there can only be one active branch.
Head must point to a branch. Head means that the most recent commit reflected in the current working directory is mapped; that is, head always points to the last commit of the active branch.
★ branch naming rule
You can use "/", but not the end of "/"
Cannot start with "-"
For components that follow "/", you cannot use "." At the beginning
You cannot use a continuous "."
Cannot use white space character ""
You cannot use symbols such as "^", "~", "?", "*", "[", etc.
Must be unique, the name of the branch always points to the last commit of the target branch
★ git branch: list, create, and delete branches
Git branch BRANCH_NAME [START_COMMIT]
Git branch-d BRANCH_NAME delete branch
★ git show-branch: view branches and their associated submissions
★ git checkout
Git checkout check out branch
Demonstrate 1:git to create a branch
[root@node1 test] # lsfirst.sh INSTALL readmin subdir [root@node1 test] # git branch-- list* master # indicates the current branch [root@node1 test] # git logcommit 3c0b6864718ec8f8aebb5b66fbfd65b757504169 (HEAD-> master) Author: watao Date: Mon Aug 19 23:06:00 2019 + 0800 v0.0.2commit b0e9cc432d3adb683963686a2eec197129ef48b8Author: watao Date: Tue Jul 16 23:26:38 2019 + 0800 v0.0.1 [root@node1 test] # git statusOn branch masterChanges to be committed: (use "git reset HEAD..." To unstage) new file: first.shUntracked files: (use "git add..." To include in what will be committed) readmin [root@node1 test] # git commit-m 'v1.0' [master 13051f2] v1.01 file changed, 4 insertions (+) create mode 100644 first.sh [root@node1 test] # git statusOn branch masterUntracked files: (use "git add..." To include in what will be committed) readminnothing added to commit but untracked files present (use "git add" to track) [root@node1 test] # git ls-files-s100644 b6a56662e48ee60ef2d65fd2b00dd555e758b7fa 0 INSTALL100644 1809e7ac283fd186a50d97b1462d2d27396f9b42 0 first.sh100644 a4ec1ecd97368714ba8b5c8d002b1a24647bb7ec 0 subdir/1.txt [root@node1 test] # git add readmin [root@node1 test] # git ls-files-s100644 b6a56662e48ee60ef2d65fd2b00dd555e758b7fa 0 INSTALL100644 1809e7ac283fd186a50d97b1462d2d27396f9b42 0 first.sh100644 8bf9463e75dfb20077fafb8358ee3cb471dd7900 0 readmin100644 a4ec1ecd97368714ba8b5c8d002b1a24647bb7ec 0 subdir/1.txt [ Root@node1 test] # git commit-m 'v1.1' [master e522483] v1.1 1 file changed 1 insertion (+) create mode 100644 readmin [root@node1 test] # git logcommit e522483313931f3dbc914ff6e132f5cd205f7d62 (HEAD-> master) Author: watao Date: Wed Aug 21 22:48:52 2019 + 0800 v1.1commit 13051f2788b17981a72f641e6b30bc8bea0f2e38Author: watao Date: Wed Aug 21 22:46:23 2019 + 0800 v1.0commit 3c0b6864718ec8f8aebb5b66fbfd65b757504169Author: watao Date: Mon Aug 19 23:06:00 2019 + 0800 v0.0.2commit b0e9cc432d3adb683963686a2eec197129ef48b8Author: watao Date: Tue Jul 16 23:26:38 2019 + 0800 v0.0.1 [root@node1 test] # git branch* master # indicates that version 1.0 creates a branch [root@node1 test] # git branch dev 13051f [root@node1 test] # git branch dev* master [root@node1 test] # git branch bug/first [root@node1 test] # git branch bug/first dev* master
Demo 2:git show-branch View Branch
# git show-branch to view specific branch information [root@node1 test] # git show-branch! [bug/first] v1.1! [dev] v1.0 * [master] v1.1 Murray + * [bug/first] v1.1 git show-branch bug/first [bug/first] v1.1 [dev] v1.0 [root@node1 test] # git show-branch dev [root@node1 test] v1.0 [root@node1 test] # git show-branch bug/first [bug/first] v1.1
Demonstrate 3:git checkout BRANCH_NAME switching branches
[root@node1 taotao] # git checkout dev # switch branch Switched to branch 'dev' [root@node1 taotao] # [root@node1 taotao] # git branch-- list bug/first* dev master [root@node1 taotao] # [root@node1 taotao] # git show-branch! [bug/first] v1.1 * [dev] v1.0! [master] v1.1 Whitney + [bug/first] v1.1 conversation + [dev] v1.0 [root@node1 taotao] # [root@node1 taotao] # [root@node1 taotao] # git logcommit 5d4298d6fdcbb6276e69f002e7148210124e52d9 (HEAD-> dev) Author: watao Date: Wed Oct 9 22:04:34 2019 + 0800 v1.0commit b9182448b3e9503ce5fd09edb5b378f612209d01Author: watao Date: Wed Oct 9 21:05:05 2019 + 0800 v0.0.2commit 1b5d8e0fea57e85045c8d98f12e379fdc73138bcAuthor: watao Date: Tue Oct 821: 24:06 2019 + 0800v0. Hello world "echo" new date "[root@node1 taotao] # [root@node1 taotao] # git statusOn branch devChanges not staged for commit: (use" git add... " To update what will be committed) (use "git checkout--..." To discard changes in working directory) modified: first.shno changes added to commit (use "git add" and/or "git commit-a") [root@node1 taotao] # git add first.sh [root@node1 taotao] # git commit-m "v1.1-dev" [dev 587719d] v1.1-dev 1 file changed 1 insertion (+) [root@node1 taotao] # git logcommit 587719dd3920429904c2b2a24637f20ecd20c4e0 (HEAD-> dev) Author: watao Date: Fri Oct 11 22:34:41 2019 + 0800 v1.1-devcommit 5d4298d6fdcbb6276e69f002e7148210124e52d9Author: watao Date: Wed Oct 9 22:04:34 2019 + 0800 v1.0commit b9182448b3e9503ce5fd09edb5b378f612209d01Author: watao Date: Wed Oct 9 21:05:05 2019 + 0800 v0.0.2commit 1b5d8e0fea57e85045c8d98f12e379fdc73138bcAuthor: watao Date: Tue Oct 821: 24:06 2019 + 0800 v0.0.1 [root@node1 taotao] # cat first. The shroud hammer beat binram bashecho "hello world" echo "new date" # cut back to the main branch Check first.sh again and find that it is still the same. Because the master branch did not submit a new operation [root@node1 taotao] # git checkout masterSwitched to branch 'master' [root@node1 taotao] # [root@node1 taotao] # cat first.sh #! / bin/bashecho "hello world" # untracked files can see [root@node1 taotao] # git checkout devSwitched to branch' dev' [root@node1 taotao] # cat first.sh #! / bin/bashecho "hello world" echo "new date" [ Root@node1 taotao] # [root@node1 taotao] # vim second.sh [root@node1 taotao] # cat second.sh #! / bin/bashecho "second.sh" [root@node1 taotao] # lsfirst.sh INSTALL second.sh subdir [root@node1 taotao] # [root@node1 taotao] # git checkout masterSwitched to branch 'master' [root@node1 taotao] # [root@node1 taotao] # lsfirst.sh INSTALL readmin second.sh subdir [root@node1 taotao] # cat second.sh #! / bin/bashecho " Second.sh "# added to the index found that [root@node1 taotao] # git add second.sh [root@node1 taotao] # git statusOn branch masterChanges to be committed: (use" git reset HEAD... ") is still visible on all branches. To unstage) new file: second.sh [root@node1 taotao] # git checkout devA second.shSwitched to branch 'dev' [root@node1 taotao] # git statusOn branch devChanges to be committed: (use "git reset HEAD..." To unstage) new file: second.sh # submit [root@node1 taotao] # git commit-m "v1.1.1-dev" [dev 21a0411] v1.1.1-dev 1 file changed, 2 insertions (+) create mode 100644 second.sh [root@node1 taotao] # git statusOn branch devnothing to commit on the dev branch, and working tree clean# switches back to the master branch It is found that second.sh has lost [root@node1 taotao] # git checkout masterSwitched to branch 'master' [root@node1 taotao] # lsfirst.sh INSTALL readmin subdir [root@node1 taotao] # git statusOn branch masternothing to commit. Working tree clean# creates a second file in the main branch, and then switches back to the dev branch to report an error. Because of a conflict with the submitted second in the dev branch [root@node1 taotao] # echo "# XIUXIU" > second.sh [root@node1 taotao] # git statusOn branch masterUntracked files: (use "git add..." To include in what will be committed) second.shnothing added to commit but untracked files present (use "git add" to track) [root@node1 taotao] # git checkout deverror: The following untracked working tree files would be overwritten by checkout:second.shPlease move or remove them before you switch branches.Aborting [root@node1 taotao] # git add second.sh [root@node1 taotao] # git checkout deverror: Your local changes to the following files would be overwritten by checkout:second.shPlease commit your changes or stash them before you switch branches.Aborting# after deleting second from the main branch of master Successfully switch to the dev branch [root@node1 taotao] # git rm-f second.shrm 'second.sh' [root@node1 taotao] # git checkout devSwitched to branch' dev' [root@node1 taotao] # git statusOn branch devnothing to commit, working tree clean [root@node1 taotao] # git show-branch! [bug/first] v1.1 * [dev] v1.1.1-dev! [master] v1.1 master-* [dev] v1.1.1-dev * [dev^] v1.1ludev+ + [bug/first] v1.1 conversation + [dev~2] v1.0 [root@node1 taotao] # git show-branch-- more=10! [bug/first] v1.1 * [dev] v1.1.1-dev! [master] v1.1 Murray-* [dev] v1.1.1-dev * [dev^] v1.1 procure + [bug/first] v1.1 conversation + [dev~2] v1.0 conversation + [dev~3] v0.0.2 conversation + [dev~4] v0.0.1
Demonstrate 4:git checkout-d BRANCH_NAME to delete a branch
[root@node1 taotao] # git branch-d bug/first Deleted branch bug/first (was 5685269). [root@node1 taotao] # git branch-- list dev* masterGit branch merge
1. Branch merge-git merge
Related concepts of ★
Merge basis: the most recent joint submission of the branch to be merged
Our version: the most recent submission of the current branch
Their version: the last submission of the branch to be merged
☉ collision-free merge:
Git checkout master; checked out to main branch
Git status checks if there is any unsubmitted information
Git merge BRANCH_NAME merged from their version to our version
Git log-- graph-- pretty=oneline-- abbrev-commit view graphically
☉ has conflicting merge:
Resolve conflicts manually
After the solution is completed: git add-- > git commit
☉ rollback to the pre-merge version
Git reset-hard ORIG_HEAD
Demo 1: merge without conflict
[root@node1 taotao] # git branch-- list dev* master [root@node1 taotao] # [root@node1 taotao] # [root@node1 taotao] # git merge devMerge made by the 'recursive' strategy. First.sh | 1 + second.sh | 2 + + 2 files changed, 3 insertions (+) create mode 100644 second.sh [root@node1 taotao] # [root@node1 taotao] # git statusOn branch masternothing to commit, working tree clean [root@node1 taotao] # git show-branch! [dev] v1.1.1-dev * [master] Merge branch 'dev'---[master] Merge branch' dev'+* [dev] v1.1.1-dev [root@node1 taotao] # git log-graph-pretty=oneline-abbrev-commit* 43d8e9a (HEAD-> master) Merge branch 'dev' |\ | * 21a0411 (dev) v1.1.1-dev | * 587719d v1.1dev* | 5685269 v1.1 | / * 5d4298d v1.0 * b918244 v0.2* 1b5d8e0 v0.0.1
Demo 2: merge with conflicts
[root@node1 taotao] # echo "taotaohuihui" > my.txt [root@node1 taotao] # git add my.txt [root@node1 taotao] # git commit-m "v2.0" [master 1df3da7] v2.0 1 file changed 1 insertion (+) create mode 100644 my.txt [root@node1 taotao] # git log-- graph-- pretty=oneline-- abbrev-commit* 1df3da7 (HEAD-> master) v2.0 # newly created submission * 43d8e9a Merge branch 'dev' |\ | * 21a0411 (dev) v1.1.1-dev | * 587719d v1.1 dev' * | 5685269 v1.1 | / * 5d4298d v1.0 * b918244 v0.2* 1b5d8e0 v0.0.1 [root@node1 taotao] # git branch devfatal: A branch named' dev' already exists. # dev branches exist that cannot create the same, but can actually be deleted, because they have been merged into the master branch [root@node1 taotao] # [root@node1 taotao] # [root@node1 taotao] # git branch fotfix # on the basis of master 2.0, the common fotfix branch [root@node1 taotao] # git show-branch! [dev] v1.1.1-dev! [fotfix] v2.0 * [master] v2.0 UV-+ * [fotfix] v2.0 muri-[Fotfix^] Merge branch 'dev'++* [dev] v1.1.1-dev [root@node1 taotao] # echo "third line" > > first.sh # modify the contents of the master branch [root@node1 taotao] # cat first.sh #! / bin/bashecho "hello world" echo "new date" third line [root@node1 taotao] # git add first.sh [root @ node1 taotao] # git commit-m "v2.1" [master 697a6c5] v2.1 1 file changed 1 insertion (+) [root@node1 taotao] # git show-branch! [dev] v1.1.1-dev! [fotfix] v2.0 * [master] v2.1 UV-* [master] v2.1 + * [fotfix] v2.0-- [fotfix^] Merge branch 'dev'++* [dev] v1.1.1-dev [root@node1 taotao] # git checkout fotfix # switch to fotfix branch Switched to branch' fotfix' [root@node1 taotao] # lsfirst.sh INSTALL my.txt readmin second.sh subdir [root@node1 taotao] # [root@node1 taotao] # echo " New line "> > first.sh# edits the first file and submits [root@node1 taotao] # cat first.shroud bins Bash echo" hello world "echo" new date "new line [root@node1 taotao] # git add first.sh [root@node1 taotao] # git commit-m" v2.0-1 "[fotfix 26b42ef] v2.0-1 1 file changed 1 insertion (+) [root@node1 taotao] # git show-branch! [dev] v1.1.1-dev * [fotfix] v2.0-1! [master] v2.1 master-* [fotfix] v2.0-1 + [master] v2.1 * + [fotfix^] v2.0-- [fotfix~2] Merge branch 'dev'+*+ [dev] v1.1.1-dev# cut back to the main branch, and then merge the fotfix branches to find an error, that is, the content conflict occurs. You need to manually fix the conflict and merge [root@node1 taotao] # git checkout masterSwitched to branch 'master' [root@node1 taotao] # [root@node1 taotao] # [root@node1 taotao] # git merge fotfix Auto-merging first.shCONFLICT (content): Merge conflict in first.shAutomatic merge failed; fix conflicts and then commit the result. Cannot merge successful files It is found that the first.sh content is different in the three versions [root@node1 taotao] # git ls-filesINSTALLfirst.shfirst.shfirst.shmy.txtreadminsecond.shsubdir/1.txt [root@node1 taotao] # [root@node1 taotao] # git ls-files-- unmerged100644 816a1b1e686cf6adb716b10e1a9eed870b2f18fa 1first.sh # "1" represents the merged base 100644 3adec3b3bf2616c52eaba36bd8af2e79e9ef6495 2first.sh # "2" represents our version 100644 e45ccd5a231f9debdd84ffd621cdc3acfc8d15f1 3first.sh # "3" represents their version [root] @ node1 taotao] # cat first.sh #! / bin/bashecho "hello world" echo "new date" > fotfix [root@node1 taotao] # git diffdiff-- cc first.shindex 3adec3b E45cccd5... 0000000 First.shallows for e45ccd5... 0000000First.shallows @-1 bin/bash echo 4-1 echo 4 + @ #! / bin/bash echo "hello world" first "new date" + + > fotfix # is the most recent submission of other editions # can be modified directly on the basis of conflicting files Delete [root@node1 taotao] # vim first.sh [root@node1 taotao] # git diffdiff-cc first.shindex 3adec3b if you want to keep them all. E45cccd5... 0000000First.shallows @-1 new date 4-1 hello world 4 + 1 echo 5 @ @ #! / bin/bashecho "hello world" echo "new date" + two lines added by third line# + new line [root@node1 taotao] # cat first.sh #! / bin/bashecho "hello world" first "new date" third linenew line# modified and re-added to the index and submitted to [root@node1 taotao] # git add first.sh [root@node1 Taotao] # git commit # content prompt appears Save it [master dd73764] Merge branch 'fotfix' [root@node1 taotao] # git log-- graph-- pretty=oneline-- abbrev-commit* dd73764 (HEAD-> master) Merge branch' fotfix' |\ | * 26b42ef (fotfix) v2.0-1 * | 697a6c5 v2.1 | / * 1df3da7 v2.0 * 43d8e9a Merge branch 'dev' |\ | * 21a0411 (dev) v1.1.1-dev | * 587719d v1.1 v1.1 | / * 5d4298d v1.0 * b918244 v0.2* 1b5d8e0 v0.0.1
Demo 3: go back to the pre-merge version
# go back to the previous version [root@node1 taotao] # git reset-- hard ORIG_HEADHEAD is now at 697a6c5 v2.1 [root@node1 taotao] # git log-- graph-- pretty=oneline-- abbrev-commit* 697a6c5 (HEAD-> master) v2.1 * 1df3da7 v2.0 * 43d8e9a Merge branch 'dev' |\ | * 21a0411 (dev) v1.1.1-dev | * 587719d v1.1 * | 5685269 v1.1 | / * 5d4298d v1.0 * b918244 v0.0.2 * 1b5d8e0 v0.0.1 [root@node1 taotao] # git show-branch! [dev] v1.1.1-dev! [fotfix] v2.0-1 * [master] v2.1 Murray-+ [fotfix] v2.0-1 * [master] v2.1 + * [fotfix^] v2.0-- [fotfix~2] Merge branch 'dev'++* [dev] v1.1.1-dev [root@node1 taotao] # cat first.sh #! / bin/bashecho "hello world" echo "new date" third line # the original file is still in [root@node1 taotao] # git checkout fotfix Switched To branch 'fotfix' [root@node1 taotao] # cat first.sh #! / bin/bashecho "hello world" echo "new date" new line # the original file is still there
Merging of 1.git variable Base branches-git rebase
★ change base operation
$git checkout dev: first switch to the branch that you want to change base
Git rebase master: converts his base from the most recent common ancestor submission to the latest submission of master
$git checkout master: back to the master branch
$git merge-m "MSG": master gets the new submission.
Demo:
[root@node1 taotao] # git checkout masterSwitched to branch 'master' [root@node1 taotao] # lsfirst.sh INSTALL my.txt readmin second.sh subdir [root@node1 taotao] # cat first.sh #! / bin/bashecho "hello world" echo "new date" third line [root@node1 taotao] # vim first.sh [root@node1 taotao] # cat first.sh # Editing the file first resolve the conflict To demonstrate the git rebasing operation #! / bin/bashecho "hello world" echo "new date" # submit [root@node1 taotao] # git add first.sh [root@node1 taotao] # git commit-m "v2.2" [master 7961be2] v2.21 file changed again 1 deletion (-) [root@node1 taotao] # git log-- graph-- pretty=oneline-- abbrev-commit* 7961be2 (HEAD-> master) v2.2 * 697a6c5 v2.1 * 1df3da7 v2.0 * 43d8e9a Merge branch 'dev' |\ | * 21a0411 (dev) v1.1.1-dev | * 587719d v1.1 * | 5685269 v1.1 | / * 5d4298d v1.0 * b918244 v0.0.2 * 1b5d8e0 v0.0.0." switch to the branch to be based [root@node1 taotao ] # git checkout fotfixSwitched to branch 'fotfix' [root@node1 taotao] # lsfirst.sh INSTALL my.txt readmin second.sh subdir [root@node1 taotao] # cat first.sh #! / bin/bashecho "hello world" echo "new date" new line # add a new line # to perform the change base operation Indicates the last submission to change the current fotfix base into a master branch [root@node1 taotao] # git rebase masterFirst, rewinding head to replay your work on top of it...Applying: v2.0-1 [root@node1 taotao] # cat first.sh #! / bin/bashecho "hello world" echo "new date" new line [root@node1 taotao] # git checkout masterSwitched to branch 'master' [root@node1 taotao] # cat first.sh #! / bin/bashecho "hello world" echo "new date" # there are no new lines at this time Need to perform merge operation [root@node1 taotao] # git merge fotfix Updating 7961be2..3eaaeadFast-forward # Fast forward merge first.sh | 1 + 1 file changed, 1 insertion (+) [root@node1 taotao] # cat first.sh #! / bin/bashecho "hello world" echo "new date" new line [root@node1 taotao] # git log-graph-pretty=oneline-abbrev-commit* 3eaaead (HEAD-> master) Fotfix) v2.0-1 * 7961be2 v2.2 * 697a6c5 v2.1 * 1df3da7 v2.0 * 43d8e9a Merge branch 'dev' |\ | * 21a0411 (dev) v1.1.1-dev | * 587719d v1.1ludev* | 5685269 v1.1 | / * 5d4298d v1.0 * b918244 v0.0.2 * 1b5d8e0 v0.0.1
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.