Git: Reset __git for Branch &&head

Source: Internet
Author: User
Tags sha1 hash
Reset of Branch

question: when there is a new commit, the contents of the reference file for the master branch change. The contents of the reference file for the master branch can be artificially altered.
Touch New-commit.txt
git add new-commit.txt
Git commit-m "Judge"
Cat. Git/refs/heads/master  #发现内容确实改变了
git log--graph--oneline   #查看提交日志 and saw just submitted
Git reset can point the master reference to any one of the existing commit IDs. (the--hard parameter destroys uncommitted changes in the workspace):
git reset--hard head^
Cat. Git/refs/heads/master   #查看master内容发现内容已改变, LS found the newly built file is still in (1.9.4.msysgit.2)
Note: Resetting the command is dangerous because resetting makes the history of submission change too, and it cannot be recovered by browsing the submission history. But->
Git provides a rescue mechanism :. The log file in the Git/logs directory Records branch changes, and more non-bare version libraries provide branch log functionality:
Because the output of the GIT config core.logallrefupdates command is true.
Tail-5. Git/logs/refs/heads/master #查看master分支的目录文件发现, which records the changes that the master branch points to, the latest changes
                                    #添加到文件的末尾.
GIT provides the git reflog command to operate on. Git/logs/refs/heads/master :
Display the contents of this file:
        git reflog Show Master | Head-5   #将最新的改变放在最前面显示
        The meaning of the #输出中表达式 <refname>@{<n>}: A SHA1 hash value that refers to the first <n> second change before <refname>. eg:a0c641e master{3}: ...
Reset Master to the value before two changes:
        git Reset--hard master@{2}
Delve into git reset command:
    Git reset has three different uses:
        git reset [q] [<tree-ish>] [--] <paths>
        git Reset (--patch |-p) [<tree-ish>] [--] [<paths> ...]
        git reset [--soft |--mixed [-n] |--hard |--merge |--keep] [-Q] [<commit>]
1 contains a <paths> usage that does not reset the reference, nor does it change the workspace, replacing the file in registers with the specified submit status <commit> file <paths>.
2 does not include the use of <paths> will reset the reference, depending on the options can be registers or workspace reset.
for 2):
    * Use parameter--hard: reference points to the new commit ID; Replaces the registers, workspace, and registers head with the same directory tree contents.
    * Use the Parameters--soft:git reset--soft <commit> only change the reference point (Index), do not change the registers and workspace
    *--mixed (default) or not applicable: Git reset <commit> change references to point to, reset registers, but do not change workspace
One use of a reset command is to modify a cursor pointing to a reference (such as Master), which actually does not use any parameters to reset the branch name (such as master) when the Reset command is executed
is set because the Reset command is directed to the head pointer. The reset command did not change the contents of the head pointer, because it points to a reference refs/heads/master,
So the reset command is embodied in the branch "cursor Change", and the head itself has been pointing to the Refs/heads/master branch and not changing in the reset.
############################################################################################################### #################
Git checkout

The head reset is checked out:
Git reset keeps the contents of the head from changing and pointing to the master branch. The essence of Git checkout is to modify the direction of the head itself, which does not
Affect branch "Cursors" (such as Master)
View the current head's point:
        Cat. Git/head   #输出ref: Refs/heads/master
Performing Git branch-v will see the current 624c083 in the Master branch.
Check out the parent commit for this ID:
        git checkout 624c083^
The output tells us that it is now in the "detached head pointer" state, where the head pointer points to a specific commit ID (available cat. Git/head view), not a reference (branch).
You can also view the latest submitted Reflog and find that the head pointer has been changed from pointing to the master branch to pointing to a commit ID:
        $ git reflog-1  #数字1
Now look at the submission ID of head and master, and find that it points differently:
        $ git rev-parse Head master   #分支master的指向没有改变, also point to the original commit ID
The head in the version library now points to e695606 and makes a change:
        Touch Detached-commit.txt
        git add detached-commit.txt
To view the status, the output says that it is not currently in any branch because the head is in Split header pointer mode:
        git status
Submitted:  #会出现警示
    Git commit-m "commit in detached head mode."
View the head pointer, pointing to the new commit:
        Cat. Git/head
Re-viewing the log will find that the new submission is based on the previous submission:
        git log--graph--pretty=oneline #记下新的提交id acc2f69
Switch the master branch name to the master branch as a parameter:
        $ git Checkout Master
The head pointer is again pointing to the branch instead of separating the head pointer mode:
        $ cat. Git/head  #输出: Ref:refs/heads/master
After the switch, the new locally created file Detached-commit.txt disappeared (LS), the log submitted just now is gone.
Take a look at the submission ID you just noted, and find that the submission is still in the object library of the version library:
        Git show acc2f69
#由于这个提交没有被任何分支跟踪到, the commit is deleted from the version library at any time after the Reflog contains the submitted log expiration.
#######################################################################
① test submissions in Split head pointer mode cannot be accessed through the master branch or other references except with the Submit ID access.
② If this submission is required by the master branch, if you reset it with Git, you can reset it, but discard the original submission from the Master branch.
★ Use the merge operation git merge to achieve both:
To merge test submissions into the master branch:
1), confirm that you are currently in the Master branch: Git branch-v
2), perform a merge operation to merge test submissions into the current branch: git merge acc2f69
3), LS found in the workspace more than one detached-commit.txt file.
4, look at the log, see a different branch diagram, the development of the branch, and then merged.
5), view the latest submission, found that there are two parent:git cat-file-p head
#######################################################################
Learn more about git checkout commands:
Git checkout overrides the workspace.
The usage of checkout can be divided into two kinds, there is no <paths>
  git checkout [-Q] [-f] [-m] [<branch>]
  git checkout [-Q] [-f] [-M]--detach [<branch>]
  git checkout [-Q] [-f] [-M] [--detach] <commit>
  git checkout [-Q] [-f] [-M] [[-b|-b|--orphan] <new_branch>] [<start_point>] ###
  git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>
  git checkout [-p|--patch] [<tree-ish>] [--] [<paths>]
Omit <commit> The default value reset is head, and the default value checked out is registers. So: resetting is generally used to reset registers (unless using--hard),
The checkout command is primarily to overwrite the workspace (if the < commit> does not omit it, it replaces the buffer response file).
* Include path <paths> usage: Does not change the head pointer, mainly for the specified version of the file overwrite the corresponding file in the workspace. If the <COMMIT> is omitted;
Overwrites the workspace file with a registers file, otherwise overwriting the registers and workspace files with the files in the specified commit.
* Do not use path <paths> usage: Changes the head pointer, (followed by <branch> allows head to switch to a branch to be able to track the commit, otherwise still enter
Detach the head pointer state, the commit in the detached head pointer state cannot be associated with the reference, and may be lost. The main function is to switch to the branch, if omitted branch equivalent to the workspace
Perform a status check.
*## #创建和切换到新的分支, the new branch is created from the commit specified by Start_point. The new branch is also a reference under the Refs/heads namespace.
eg
git Checkout Branch # checks out the branch branch, updates the head to point to the Branch branch, and updates the branch and workspaces with the tree that the registers points to.
git checkout    #汇总显示工作区, registers and head differences, or git checkout head
git checkout--filename    #用暂存区中的filename覆盖工作区中的filename文件
git checkout branch-filename    #用branch所指向的提交中的filename覆盖暂存区和工作区中响应的文件.
Git checkout--.  or  git checkout.   #取消所有本地的修改 (relative to registers), which is equivalent to overwriting all files in registers directly to the local area.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.