Git and GitHub – Git Checkout (Part-6)

1. Switching Branches

Scenario: You have multiple branches in your repository, and you want to switch from the current branch to another branch.

Example:

  1. List Branches:
git branch

* master
  feature-branch

Switch to feature-branch:

git checkout feature-branch

Switched to branch 'feature-branch'
  • Explanation: This command switches the working directory to the feature-branch.

2. Creating and Switching to a New Branch

Scenario: You want to create a new branch called new-feature and switch to it.

Example:

  1. Create and Switch to a New Branch:
git checkout -b new-feature

Switched to a new branch 'new-feature'
  • Explanation: The -b flag creates a new branch called new-feature and switches to it.

List Branches to Verify:

git branch

  feature-branch
* new-feature
  master
  • Explanation: The new-feature branch is now the current branch (indicated by *).

3. Checking Out a Specific Commit

Scenario: You want to view the state of the repository at a specific commit.

Example:

  1. Get the Commit Hash:
git log

commit 7062f9ba83b7f96f3b722be237935c0c30ac5de4 (HEAD -> master)
Author: Jami Raj <jami.cotocus@gmail.com>
Date:   Tue May 28 16:07:13 2024 -0700

    all

commit 3d0f84561e39cdfc6724c77b2b135e245dc6bf22
Author: Jami Raj <jami.cotocus@gmail.com>
Date:   Tue May 28 16:05:15 2024 -0700

    file1
  • Explanation: Note the commit hash you want to check out (e.g., 3d0f84561e39cdfc6724c77b2b135e245dc6bf22).
git checkout 3d0f84561e39cdfc6724c77b2b135e245dc6bf22

Note: switching to '3d0f84561e39cdfc6724c77b2b135e245dc6bf22'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 3d0f845 file1

Explanation: This switches the working directory to the state at the specified commit. You are in a “detached HEAD” state, meaning you are not on any branch.

Restoring a File to a Previous State

Scenario: You want to restore a specific file to its state at a previous commit.

Example:

  1. Check Out the File from a Specific Commit:
git checkout 3d0f84561e39cdfc6724c77b2b135e245dc6bf22 -- file1.java

Output:
Updated 1 path from 3d0f845

Explanation: This restores file1.java to its state at the specified commit without switching the entire working directory to that commit.

Related Posts

Git and GitHub – GitHub Overview (Part-15)

1. Github Description: 2. Org (Organization) Description: Example: 3. Team Description: Example: 4. Auth (Authentication) Description: Examples: OAuth Authentication: Personal Access Tokens: SSH Key Authentication: 5. Action…

Read More

Git and GitHub – Git Log Explanation (Part-14)

Git Log Explanation Explanation of Each Part 1. Commit Hash 2. References (Branches and Tags) 3. Commit Message Simplified Breakdown of Each Line Summary The git log…

Read More

Git and GitHub – Merge and Pull (Part-13)

Merge and Pull in Git Merge Merge is a command used to combine the changes from one branch into another. It is typically used to integrate changes…

Read More

Git and GitHub – Types of branch (Part-12)

Local and Remote Branches in Git Local Branch A local branch is a branch that exists in your local Git repository. You create and manage local branches…

Read More

Git and GitHub – Git Undoing, Conflict Resolution, Cherry Picking, Stash (Part-11)

Undoing changes in Git can be approached in several ways, depending on what you need to achieve. Here, we’ll cover the most common scenarios and commands for…

Read More

Git and GitHub – Understanding Git Concepts (Part-10)

Understanding Git Concepts 1. Git Branching and Merging Branching allows you to create a separate line of development. It’s useful for working on different features or fixes…

Read More
guest
0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x