1

Currently I have my head and master as follows-

2441dc3 (HEAD) Made backend route for student attendance
2b27490 Made storage to localstorage from the classname
577bd81 (origin/master, master) Made attendace UI working though there are some errors

How do I now make my current head the master?

3 Answers 3

4

You re-create master at the current HEAD and then check it out:

git branch -f master
git checkout master

That gives you

2441dc3 (HEAD -> master) Made backend route for student attendance
2b27490 Made storage to localstorage from the classname
577bd81 (origin/master) Made attendace UI working though there are some errors
1
  • 4
    Or, in one step, git checkout -B master or git switch -C master.
    – torek
    Commented Apr 24, 2021 at 9:04
2

You may do a hard reset to the master branch, or reset to master's latest SHA-1 hash:

# from your current branch
git reset --hard origin/master
git reset --hard 577bd81
2

HEAD?

HEAD is simply a reference to the current commit (latest) on the current branch.
There can only be a single HEAD at any given time (excluding git worktree).

The content of HEAD is stored inside .git/HEAD and it contains the 40 bytes SHA-1 of the current commit.


git checkout

git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits to go back

This will checkout new branch pointing to the desired commit.
This command will checkout to a given commit.
At this point, you can create a branch and start to work from this point on.

# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>

# Create a new branch forked to the given commit
git checkout -b <branch name>

git reset --hard <commit_id>

"Move" your HEAD back to the desired commit.

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts if you've modified things that were
# changed since the commit you reset to.
  • Note: ([Since Git 2.7][8]) you can also use the git rebase --no-autostash as well.

Enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.