Common usage scenarios for Git branch operations

1. Delete unnecessary branches

1. Now under the my-pro project, create several new branches for testing.

git checkout master # point HEAD to the master branch

git branch testdel #Create a new branch under the master point

git checkout testdel #point HEAD to testdel branch

#Modify style.css here, for example, add a background color background:#d8d8d8

git commit css/style.css -m'just a test commit' #Execute a commit operation under testdel

git branch testagain

git checkout master

git branch anothertest

gitk --all #Use gitk to visualize the current branch status

After multiple branch creation and commit operations, our branch has become the structure shown in the following figure:

2. Use the git branch -d [branch name] command to delete a specified branch.

Yooye-2:my-pro yooye$ git branch -av #View current branch status

anothertest dc7ecf7 let index.html link the style file

change-border-color 413552e change the box border-color

* master dc7ecf7 let index.html link the style file

testagain e1e471a just a test

testdel e1e471a just a test

Yooye-2:my-pro yooye$ git checkout master #Enter the master branch

Switched to branch 'master'

Yooye-2:my-pro yooye$ git branch -d testagain #Use -d to delete a branch

error: The branch 'testagain' is not fully merged.

If you are sure you want to delete it, run 'git branch -D testagain'. #Prompt us to use -D to delete

Yooye-2:my-pro yooye$ git branch -D testagain #Use -D to try to delete a branch again

Deleted branch testagain (was e1e471a). #Delete successfully

Yooye-2:my-pro yooye$ gitk --all #View the visual branch status after deletion

The state after deleting the testagain point is as follows:

 

 3. When we use git checkout to switch between testdel and master branches, code files such as style.css in the project will switch accordingly. If the testdel branch is deleted, all commit operation records submitted under this branch will be removed. So we must do version confirmation before deleting the branch.

 2. Modify the description of the latest commit

Use the git commit --amend command to modify the descriptive text of the most recent commit. The operation process is as follows:

Yooye-2:my-pro yooye$ git checkout testdel #【1】Enter testdel branch

Switched to branch 'testdel'

Yooye-2:my-pro yooye$ git log -1 #【2】Check the latest commit record, note that this is git log -[Number 1]

commit e1e471a5945ab83673bc56055e3455e9fbec8e61 (HEAD -> testdel)

Author:  <[email protected]>

Date: Fri Mar 1 10:26:49 2019 +0800

just a test # [2-1] This is the description text before modification

Yooye-2:my-pro yooye$ git commit --amend # [3] Execute the modification command and enter the modification state. See step [4] below for detailed operation

Yooye-2:my-pro yooye$ git log -1 #【5】Check to see if the modification is successful

commit 1062fa43b85ad7111738d4eaee56436279768757 (HEAD -> testdel)

Author:  <[email protected]>

Date: Fri Mar 1 10:26:49 2019 +0800

just a test and i had change this words 【5-1】modified description text

Yooye-2:my-pro yooye$

[4] Execute the git commit --amend modification command, the modification method is as follows:

 

 3. Modify the old commit description information

Use the git rebase -i [unique id of the modified parent commit] command to enter the rebase operation.

[Note]: The parent-level commit is used above to enter the rebase operation, and the operation is to enter the commit under the parent level.

1. Because a variety of operations can be supported in the rebase state, here we need to change the default pick operation mode in front of the commit that needs to be modified to reword (that is, the meaning of modification). Then save and exit in the same way as in the second step, and enter the next operation interface.

2. In this operation interface, the description content of the original commit can be modified, as shown in the first line of the figure below. Then continue to save and exit.

 

 3. Use git log --graph to view the modified results, as shown in the figure below:

4. Merge multiple consecutive commit actions

According to the results of git log --graph in the previous step, it can be seen that there are actually three commits above to show the index.html in a normal manner, so we can merge the other three commits except for the readme operation .

1. Use the git rebase -i [the unique id of the commit that first created the readme] command to enter the operation interface. Merge these three commits into one of them, then save and exit to the next step.

pick 789f15c add a index.html file #[1] means merge other commits into this commit

squash e7bf7b0 add aaaaaaa style file #【2】merge

squash 4216faf let index.html link the style file #【3】merge

# Rebase 1bb42bc..4216faf onto 1bb42bc (3 commands)

#

# Commands:

# p, pick <commit> = use commit

# r, reword <commit> = use commit, but edit the commit message

# e, edit <commit> = use commit, but stop for amending

# s, squash <commit> = use commit, but meld into previous commit

# f, fixup <commit> = like "squash", but discard this commit's log message

# x, exec <command> = run command (the rest of the line) using shell

# d, drop <commit> = remove commit

# l, label <label> = label current HEAD with a name

# t, reset <label> = reset HEAD to a label

# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]

# . create a merge commit using the original merge commit's

# . message (or the oneline, if no original merge commit was

# . specified). Use -c <commit> to reword the commit message.

#

# These lines can be re-ordered; they are executed from top to bottom.

# If you remove a line here THAT COMMIT WILL BE LOST.

# However, if you remove everything, the rebase will be aborted.

#

#

# Note that empty commits are commented out

~

:wq!

2. Fill in the description text of this merger, and keep the description text of the original commit for subsequent viewing.

# This is the 1st commit messages:

I am trying to make a combination, Yeah! #【1】The description text of this merger operation

add a index.html file # [2-1] Reserved original commit description text

# This is the commit message #2:

add aaaaaaa style file # [2-2] Reserved original commit description text

# This is the commit message #3:

let index.html link the style file # [2-3] Reserved original commit description text

# Please enter the commit message for your changes. Lines starting

# with '#' will be ignored, and an empty message aborts the commit.

#

# Date: Wed Feb 27 11:42:12 2019 +0800

#

# interactive rebase in progress; onto 1bb42bc

# Last commands done (3 commands done):

# squash e7bf7b0 add aaaaaaa style file

# squash 4216faf let index.html link the style file

# No commands remaining.

# You are currently rebasing branch 'anothertest' on '1bb42bc'.

#

# Changes to be committed:

# new file: css/style.css

# new file: index.html

#

# Untracked files:

# js/

#

:wq!

3. Use git log --graph to view the status of the merged version.

5. Merge discontinuous commits

1. Based on the previous step, in order to demonstrate, we remove all other test branches, and only keep the original master branch, and three commits related to index.html, style.css, and readme.md.

Then we modify the content of the readme.md file under my-pro, and perform a commit operation. Then use the git log --graph command to view the current commit status list.

 

 

2. Because the first and last commits are all about readme submissions, we need to merge them. Before executing git rebase -i [the lowest ancestor commit], remember to copy the id of the ancestor commit (eg: 1bb42bc) for future use.

After entering the operation window, perform operations as shown in the following code to save and exit.

pick 1bb42bc8 #[1] This sentence is newly added by us, which is to merge the commit operation of another readme operation

squash 702dcbf I am the real readme.md file # [2] This line was originally at the bottom, we moved it to this, and changed the pick to squash

pick 2b2e247 I am trying to make a combination, Yeah!

# Rebase 1bb42bc..702dcbf onto 1bb42bc (2 commands)

#

# Commands: There were a lot of command prompts here, which were deleted

# These lines can be re-ordered; they are executed from top to bottom.

# If you remove a line here THAT COMMIT WILL BE LOST.

# However, if you remove everything, the rebase will be aborted.

#

:wq!

3. If the reminder branch is not continuous after saving and exiting, we can use git rebase --continue to continue to the next step.

6. Compare the difference between the temporary storage area and the files contained in HEAD

1. Modify the content of index.html in the my-pro warehouse.

2. Perform a git add operation.

3. Use git diff -cached to view the difference between the temporary storage area and the files contained in HEAD.

Yooye-2:my-pro yooye$ git diff --cached #【1】Execute comparison command

diff --git a/index.html b/index.html

index 084901f..ec530c2 100644

--- a/index.html

+++ b/index.html

@@ -5,7 +5,7 @@

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<link rel="stylesheet" href="css/style.css">

- <title>Document</title> #【2-1】The old content pointed to by HEAD before no change

+ <title>Learn Git</title> #【2-2】The content added to the temporary storage area through git add after modification

</head>

<body>

<div class="box"></div>

7. Compare the difference between the files contained in the work area and the temporary storage area

1. Based on the previous step, let’s adjust the style.css file (such as adding a background), and then do not execute the git add operation. At this time, the changes to the style.css file we just edited belong to the workspace.

2. Execute the git diff command to view the differences between the workspace and the previously submitted staging area files.

Yooye-2:my-pro yooye$ git diff

diff --git a/css/style.css b/css/style.css

index 20ce14c..488853c 100644

--- a/css/style.css

+++ b/css/style.css

@@ -2,4 +2,5 @@

width: 100px;

height: 100px;

border: 1px solid red;

+ background: pink;

}

\ No newline at end of file

Yooye-2:my-pro yooye$

3. If multiple files have been modified before submitting to the temporary storage area, but we only want to view the changes of a certain file, such as readme.md, we can use the following command:

git diff -- readme.md

8. Restore the content in the temporary storage area to be the same as HEAD

In the usage scenario, we modify the index.html in the sixth step and submit it to the temporary storage area through git add. If we regret it at this time, we can use the git reset HEAD command to restore it to the same as HEAD. The testing process is as follows:

Yooye-2:my-pro yooye$ git status # [1] Check the commit status before restoring

On branch master

Changes to be committed:

(use "git reset HEAD <file>..." to unstage)

modified: index.html #【2-1】Already in the temporary storage area

Changes not staged for commit:

(use "git add <file>..." to update what will be committed)

(use "git checkout -- <file>..." to discard changes in working directory)

modified: css/style.css #【2-2】Not in the temporary storage area

modified: readme.md

Yooye-2:my-pro yooye$ git reset HEAD #【3】Restore the contents of the temporary storage area

Unstaged changes after reset:

M css/style.css

M index.html

M readme.md

Yooye-2:my-pro yooye$ git status

On branch master

Changes not staged for commit:

(use "git add <file>..." to update what will be committed)

(use "git checkout -- <file>..." to discard changes in working directory)

modified: css/style.css #【4】Not in the temporary storage area

modified: index.html

modified: readme.md

no changes added to commit (use "git add" and/or "git commit -a")

 

Guess you like

Origin blog.csdn.net/longz_org_cn/article/details/130010863