Git:
- Git is a tool that helps us to track or roll back in code or versions of our code development. For example, if we are working on a project with different parts. If the implementation of today has issues, it can be rolled back to the previous version with a simple rollback.
- Git can also be used to work on projects collaboratively.
GitHub:
Two types of Configurations:
- Local: when planning to use multiple git accounts we use local.
- Global: when we are about to use a single account, then we can simply configure Global.
Operations:
In the above picture, initially, the content is not modified so it says "nothing to commit". But when we check the status for the second time it indicates that the content is altered, due to changes we made to the file.
Types of Git status:
Commit:
Git Log and Checkout: Navigating Commit History
When working with Git, understanding how to navigate through your commit history is essential for reviewing changes, debugging, or even reverting to previous states of your code. Two fundamental commands that facilitate this are git log
and git checkout
.
1. Viewing Commit History with git log
The git log
command is used to display a list of all commits made to the repository along with details like the commit hash, author, date, and commit message. This command is helpful to track the progression of changes over time.
Using git log
will show a history like this:
Each entry contains a unique hash (e.g., 1a2b3c4d5e6f7g8h9i0jklmnopqrst
), which is the identifier for that commit. You can use these hashes to reference specific points in the project's history.
2. Moving to a Specific Commit with git checkout
Once you've found the commit you want to explore, you can use git checkout
followed by the commit hash to navigate to that state of the code. This doesn’t delete any files but lets you see your code as it was at that point.
For example:
By running this command, Git will switch the files in your working directory to match the selected commit. This can be very useful if you need to review or test a previous version without altering your current branch.Note: When you checkout a specific commit, Git puts you in a "detached HEAD" state. This means you are no longer on a branch but at a specific commit. To return to the latest commit on your branch, use: git checkout main
In summary, git log
helps you view the entire commit history, while git checkout
allows you to navigate to a specific commit based on its unique hash. This combination of commands gives you full control to move through your project’s timeline, experiment with past changes, or review the evolution of your codebase.
Making Changes While on a Previous Commit
When you’re working in Git, sometimes you might want to explore changes from a previous commit. If you make edits while you’re in a detached HEAD state (which occurs when you checkout a specific commit rather than a branch), there are two paths you can take with those changes:
If You Commit the Changes: Committing changes while in the detached HEAD state will create a new branch starting from that point. This means you’ve essentially branched off from your main history, and Git will treat it as an alternative history. In this scenario, the
HEAD
pointer will move to the new commit, but your main branch will remain as it was, unaffected by these new changes. This can be useful if you want to explore a different direction in your project without altering the main branch.If You Don’t Want to Keep the Changes: If you decide the changes aren’t needed and you just want to return to the main branch’s latest state, you can use
git checkout -f main
. The-f
(force) option here tells Git to discard any uncommitted changes made during your time on the previous commit, ensuring a clean transition back to the main branch’s latest commit. This way, any edits made while exploring the older commit are deleted, and you’re brought back to the most recent state of your main branch.
Using git checkout -f main
to Discard Changes and Return to the Latest Commit:
The git checkout -f main
command is your go-to option if you’ve made changes in the detached HEAD state and want to abandon them altogether. It discards any modifications and takes you back to the latest commit on the main branch, ensuring that no temporary changes from the previous commit are carried over.
This ability to check out older commits, make experimental changes, and then either commit to a new branch or discard changes, highlights Git’s versatility, giving you full control over your project’s history without impacting your main development flow.
When you create a new commit after checking out a previous commit (detached HEAD state) and then switch back to main
, the new commit might not be linked to any branch. Here’s how you can get back to it:
Steps to Recover Your Commit
Find the Commit Hash
- Git retains the new commit, even if it’s not attached to a branch. Use the following command to find it:
- This command lists all actions, including your newly created commit.
- Look for an entry like:
- Git retains the new commit, even if it’s not attached to a branch. Use the following command to find it:
Switch Back to the Commit
- Once you have the commit hash, use:
- Once you have the commit hash, use:
Create a Branch for the Commit
- To ensure you don’t lose this commit again, create a branch:
- This attaches the commit to a branch for easier access in the future.
- To ensure you don’t lose this commit again, create a branch:
Checkout the New Branch
- Move to the new branch:
- Move to the new branch:
Example Workflow
Suppose you make a new commit in a detached HEAD state:
You switch back to
main
:Later, you want to recover your previous commit:
Run
git reflog
:Identify the commit hash (
def5678
).Checkout the commit:
Create a branch:
INIT:
Push:
Git Restore:
How Does git restore
Work?
Let’s go step by step with examples to make it clear.
Restoring a File to Its Last Committed State: Imagine you have a file called
example.txt
that you modified, but you now want to discard those changes.This command reverts
example.txt
to its state in the last commit.Unstaging a File: Suppose you’ve added changes to the staging area using
git add
, but now you want to unstage them.This command removes
example.txt
from the staging area without affecting your modifications in the working directory.Restoring All Files: If you’ve made multiple changes across your working directory and want to discard all of them, you can run:
This reverts all modified files in the current directory to their last committed state.
Restoring a Specific Version: If you want to restore a file from a specific commit, use:
Replace
<commit-hash>
with the ID of the commit you want to restore from.
The Difference Between git restore
and git reset
Let’s clarify this because it’s a common point of confusion:
git restore
: Focuses on undoing changes to files in the working directory or staging area.git reset
: Can undo commits, unstage files, and reset the working directory—more powerful but also riskier.
Git Reset:
What is git reset
?
git reset
is used to undo changes by resetting the current branch to a specified state. It affects the staging area, your working directory, and the commit history depending on the options you use.
Think of it as a "rewind" button that can:
- Move your branch pointer to an earlier commit.
- Modify the staging area (unstage files).
- Change files in your working directory.
Types of git reset
There are three main modes of git reset
, and each has a different impact on your repository:
--soft
: Only changes the commit history (doesn't touch the staging area or working directory).--mixed
: Resets the commit history and unstages files, leaving changes in the working directory.--hard
: Resets the commit history, unstages files, and discards changes in the working directory (permanently).
How to Use git reset
Let’s go step by step with examples for each mode.
1. git reset --soft
: Roll Back Commits Without Losing Changes
Use this mode if you want to undo a commit but keep your changes staged.
Scenario: You’ve committed changes but realized you forgot to include an important update.
What it does:
- Moves the branch pointer (HEAD) back by one commit.
- Keeps the changes from the undone commit in the staging area.
You can now add the missing changes and re-commit:
2. git reset --mixed
: Unstage Changes Without Losing Them
This is the default mode of git reset
(if no flag is specified). It undoes a commit and removes changes from the staging area but keeps them in the working directory.
Scenario: You’ve staged and committed files, but you realize you need to fix some of them before committing.
What it does:
- Moves the branch pointer back by one commit.
- Unstages all the changes from that commit.
- Keeps the changes in your working directory.
You can now edit the files and stage them again as needed.
3. git reset --hard
: The Nuclear Option
Use this mode if you want to completely undo changes and discard them from your working directory.
Scenario: You’ve committed changes and realized they’re completely wrong. You want to start fresh.
What it does:
- Moves the branch pointer back by one commit.
- Unstages all changes.
- Discards all changes from the working directory.
Branching:
Understanding Branches in Git
Branches in Git are essential for managing code changes in a collaborative environment, enabling teams to work on different features, bug fixes, or tests independently without impacting the main codebase. For instance, let’s say you create a develop
branch from a specific commit. This branch can then be assigned to the development team, where they can freely make changes and add new features without affecting the main branch.
Use Cases for Branches
- Development: You can create a
develop
branch for new features and assign it to the development team. They can work on this branch, adding features and updates without interfering with the stable codebase. - Testing: Once development is complete, you might want to send the code for testing. You can create a
testing
branch, allowing the QA team to test the changes independently. - Bug Fixes: If there's an issue in production, you can create a separate
bugfix
branch from a stable point in the code. This allows the bug-fix team to address the problem without interrupting ongoing development.
Creating a Branch in Git
To create a new branch, you can use the command:
Replace <branch-name>
with the desired name of your branch. For example, git branch develop
will create a branch named develop
.
By using branches, you can organize your work effectively and manage multiple tasks in parallel, whether it’s development, testing, or bug fixes.
Switching Branches in Git
Once you have multiple branches, you can switch between them using:
For example, if you want to switch to a branch named develop
, you would use:
This command will update your working directory to reflect the state of the specified branch, allowing you to work on different tasks without affecting other branches.
In recent versions of Git, there's also an alternative command:
This is a more specific command for switching branches and works similarly to git checkout
.
Using these commands, you can easily move between branches to focus on different tasks or features in your project.
When you use:
it does two things at once:
- Creates a new branch with the name you specify.
- Switches to that branch immediately after creating it.
For example:
This will create a new branch called feature-xyz
and automatically switch you to that branch.
This command is especially useful when you want to quickly branch off to work on a new feature or bug fix without needing to type two separate commands to create and switch.In Git, when you create a new branch, it captures all the code from the point at which it was created. Here’s a breakdown of how branching works and how you can manage different branches based on your specific needs:
Comments
Post a Comment