1 branch

  • Branch names can be thought of as Pointers, such as **master, test, Origin /master **, etc., which are all Pointers to a commit snapshot
  • Git has a special pointer called HEAD, which is a pointer to the current local branch
  • Git branch: Lists all local branches, with the current branch highlighted and preceded by an asterisk
  • Git branch -d: Deletes a branch
    • Because you delete the current branch, the HEAD pointer goes nowhere
  • Git branch -d: the uppercase D indicates forcibly deleting a branch. Use this command with caution
  • Git branch name: Create a branch named “name”. Note that this branch is not switched to
  • Git checkout -b git checkout -b git checkout -b git checkout
  • Git branch -v: View the last commit for each branch
  • Git branch-vv: Displays the tracing relationship between local and remote branches

1.1 Branch Merger

When you try to merge two branches, Git simply moves the pointer forward (to the right) when merging them if you follow one branch to the other, because there is no divergence to resolve in this case — this is called “fast-forward.”

1.2 Checking Branch Merging

  • Git branch –merged: Check which branches are merged to the current branch
    • Branches that do not have an * before their names in this list can usually be removed using gitBranch -d. You’ve integrated their work into another branch, so nothing is lost.
  • Git branch –no-merged: Check which branches are not merged to the current branch
    • Git branch -d deletes will fail because the branches have not been merged yet, and deletes will lose things.

2 Remote Branch

2.1 origin/master and master

  • Origin /master: local pointer to the commit snapshot of the remote branch at the time of the last synchronization
    • As long as you are not synchronizing with the Origin server (git fetch/pull, etc.), your Origin/Master pointer will not move. See Git Remote Branch for details
  • Git fetch Origin: ** ** Synchronizes origin remote data to the local server
    • Include all branches, not just the current branch, and update the local database (but not merge to the current master branch)
    • Git fetch moves the Origin /master pointer to the updated location. See Git Remote Branch for details

2.2 Local Branch Tracing remote branches

First, you can use **git branch -vv ** to see the trace relationship between local and remote branches. There are two cases:

  • In the first case, a new branch is created based on the remote branch:
#The first method specifies the local branch name
git checkout -b test origin/test

#The second option, using the --track argument, omits the branch name, which is origin by default
git checkout --track origin/serverfix
Copy the code
  • (2) use **-u or –set-upstream-to ** while you are not using the remote branch as a base to track the remote branch:
git branch -u origin/test

#or
git branch --set-upstream-to origin/test

#Output the following:
# Branch test set up to track remote branch test from origin.
Copy the code

2.3 Deleting a Remote Branch

git push origin –delete test

3 rebase

3.1 Rebasing and merging

Git merge If you want to “merge” changes from the master branch to the experiment branch, you usually use git merge as follows:

#1. Switch to the Experiment branch
git checkout experiment

#2. Merge the master branch to the current branch
git merge master
Copy the code

Again, if we want to “integrate” changes from the Master branch into the Experiment branch, we can also use “rebase”, as follows:

#1. Switch to the Experiment branch
git checkout experiment

#2. Change the master
git rebase master

#3. The following output is displayed
# First, rewinding head to replay your work on top of it...
# Applying: added staged command 
Copy the code

The merge operation, like the merge operation, only needs to replace “merge” keyword with “rebase”; Whether it’s through rebasing or three-way merging, the end result of consolidation is the same, but the commit history is different, and the commit history of rebasing is a straight line without forks.

3.2 rebase understand

Git rebase master on the current branch (say experiment). Git rebase master on the current branch (say experiment). Git rebase master on the current branch (say experiment).

More variable base, see Git base change.

3.3 the git pull – rebase

Your branch and 'origin/master' have diverged,
and have 1 and 11 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
Copy the code

If git status encounters the above output, use git pull –rebase

4 log

Displays the last three commit hashes for the branch in reverse order

git log -3 --pretty=format:"%h" data-privilege --reverse | xargs git cherry-pick
Copy the code

4.1 the git reflog

Reflog, which stands for reference log, records the history to which your HEAD and branch references point in recent months.

Note that the reference log only exists in the local repository, a log of what you have done in your own repository. Someone else’s copy of the repository reference log will not be the same as yours; When you clone a new repository, the reference log is empty because you haven’t done anything in the repository yet. You can think of the reference log as a Git version of your shell history.

4.2 the git log since.. until

Git log since.. until

  • git log master.. Experiment: Can be used to view submissions that are in the Experiment branch but not the Master branch
    • Instead, git log experiment.. Master is used to look at submissions that are in the Master branch but not the Experiment branch

This command is usually used to solve the problem of “what other commits from this branch are not merged into the master branch”.

Git log experiment –not master git log experiment –not master git log experiment –not master git log master.. Where is experiment and where is it not?

4.3 Log Search

  • Git log-sstr –oneline: Use the -s option to display new and removed string STR commits
  • Git log-gegrep –oneline: Use the -g option to search with regular expressions

5 git stash

The git Stash command is the answer to the problem of making a change in your current branch and wanting to switch to another branch without losing the change or making a commit (perhaps halfway through and creating a meaningless commit).

The following is an example of how to do this:

    1. Some changes are made in the current branch (say experiment)
    1. Git Stash puts changes to the current branch in a “stash stack”
    1. Git Checkout Master switch to the Master branch and make other changes and commit
    1. Git checkout experiment cuts back to the experiment branch
    1. The Git Stash apply restores the changes that were recently saved in the “stash stack” to the current branch and continues

Some common git Stash commands:

  • ** Git stash: ** Save the current change in the “stash stack” (workspace and staging have no current change)
  • ** Git Stash list: ** See what is stored in the “stash stack”
  • ** Git stash apply: ** Replace changes recently saved in the “stash stack” back into the current branch
    • Note that the git stash apply command doesn’t have to be executed on the original branch. It can be executed on any branch. The result is to put the changes in the “stash stack” back into the current branch. So I can apply on the Experiment stash branch and the Master branch, so I can take the experiment changes directly to the master branch without having to commit them in the merge. This is useful when you forget what branch you are in, you change it for a long time, and then, oh my God, you change it in the Master branch (usually you can’t change it directly in the Master branch). You wanted to change it in the Dev branch, so it’s very useful to use git Stash
  • ** Stash pop: This is basically the same as the git Stash apply, except that after the git stash apply, the stash remains in the “stash stack”, while the pop “applies and pops” the stash, that is, the stash is deleted.
  • ** Git stash drop stash reference: ** Remove the stash in the stash stack

6 Rewriting history

6.1 Modifying the last submission

  • Git commit –amend: Amend the last commit and modify the commit information
  • Git commit –amend –no-edit: Modify the last commit without changing the commit information

6.2 Modifying Multiple Submissions

Use git commit — Amend can only modify the last commit, but if you want to modify the last three commits, or any of the last three commits, you can use interactive rebase: git rebase -i HEAD~3

Running this command will give you a list of submissions in a text editor that looks like this:

pick 93b2809 Initial commit
pick 76da34e macair add 1.txt
pick 3c7f123 macair add 4.txt

 #Rebase 73b2649.. 3c7f123 onto 73b2649 (3command(s))
 #
 # Commands:
 # p, pick = use commit
 # r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
Copy the code

The last three commits are displayed from old to new (the oldest commit is at the top, in reverse order of git log output). If you want to modify a commit, change the “Pick” preceding the commit to “Edit” or “E”. For example, suppose we want to modify the “76DA34E” commit, change the previous “pick” to “e”, save and exit, and the following output will appear:

Stopped at 76da34e1331826c594351381279f4bb99f14e9fc... macair add 1.txt
You can amend the commit now, with
	git commit --amend
Once you are satisfied with your changes, run
	git rebase --continue
Copy the code

You can see that you can now make changes to the commit. When you are done, run “git Commit — Amend” to change the commit, 76da34e. After modifying this commit, use “Git rebase –continue” to complete the interactive base change. The steps are summarized as follows:

    1. Modify the content
    1. git commit –amend
    1. git rebase –continue

Suppose we now modify the file and change commit MSG to “rebase test”. Git log:

* 132e4b3 - (HEAD -> macair) macair add 4.txt (4 seconds ago) <wxweven>
* 0750efb - rebase test (16 seconds ago) <wxweven>
* 93b2809 - Initial commit (22 minutes ago) <wxweven>
Copy the code

As you can see, we have made changes to the historical commit. Note that the interactive rebase produces a new COMMIT (you can see that the hash before the commit is 0750efb).

More interactive rebasing demos are shown below:

  • Merge multiple commits
#Merge multiple commitsPick F7F3F6d V1.1 changed my name a bit pick 310154e V1.2 updated README formatting and added blame pick A5F4A0d v1.3 added cat-file
#Change to the following:Pick F7f3F6d V1.1 changed my name a bit squash 310154e V1.2 updated README formatting and added blame squash a5f4A0d V1.3 added cat - fileCopy the code
  • Adjust the submission order
#Change the submission order, it was v1.1 -> V1.2 -> v1.3, want to change it to V1.1 -> V1.3 -> v1.2Pick F7F3F6d V1.1 changed my name a bit pick 310154e V1.2 updated README formatting and added blame pick A5F4A0d v1.3 added cat-file
#Change to the following :(that is, adjust the order, otherwise unchanged)Pick F7F3F6d V1.1 changed my name a bit pick a5F4A0d v1.3 added cat-file pick 310154e v1.2 updated README formatting and  added blameCopy the code

7 cherry-pick

7.1 usage

git cherry-pick commitSha

7.2 the demo

Suppose the branch and commit situation now looks like this (abcd instead of commit hash) :

a - b - c - d     Master
     \
       e - f - g  Feature
Copy the code

Suppose we now want to merge the commit “F” into the master branch, i.e., partially “merge” the feature branch into the master branch, rather than merge the whole feature branch into the master branch. Then you can use cherry-pick.

#1. Switch to the Master branch
git checkout master

#Cherry - pick submission f
git cherry-pick f
Copy the code

Now the branch commits as follows:

a - b - c - d - f' Master
     \
       e - f - g    Feature
Copy the code

The commit ‘f’ on the master branch is a new commit. The commit’ f’ on the master branch is the same as the commit ‘F’ on the feature branch.

More cherry – pick, see: [www.atlassian.com/git/tutoria]…