Wednesday, 13 March 2024

The Git & Github Bootcamp Part 7- Master on essentials and the tricky bits: rebasing, squashing, stashing, reflogs, blobs, trees, & more!


The Power of Reflogs - Retrieving “Lost” Work

1. Introducing Reflogs

Reflogs are a mechanism in Git that records updates to refs (branch heads, tags, etc.), effectively logging every state your repository has been in. This includes commits, merges, and even failed attempts at rewriting history.

2. The Limitations of Reflogs

Reflogs are local to your repository; they are not shared when you push or pull changes. They’re meant for recovering local changes that may have been lost or mistakenly altered. Additionally, entries in reflogs expire after a certain period (default is 90 days for commits that can no longer be reached and 30 days for reachable commits).


3. The Git Reflog Show Command

To see the history recorded in your reflogs:

git reflog

This command displays a list of recent activities in your repository, including commit hashes and descriptions of the actions (e.g., commit, merge, rebase).

4. Passing Reflog References Around

You can reference commits found in your reflog like any other commit in Git. For example:

git checkout HEAD@{1}

This command checks out the state of your repository one action ago, as recorded in the reflog.

5. Time-Based Reflog Qualifiers

Git allows you to reference actions based on time. For instance:

git reflog --since="yesterday"

This shows reflog entries since yesterday, helping you narrow down when a specific change was made.

6. Rescuing Lost Commits With Reflog

If you’ve accidentally deleted a branch or lost commits, you can find the last commit of the lost branch in the reflog:

  1. Use git reflog to find the hash of the lost commit.
  2. Check out the commit or create a new branch from it:
    git checkout -b recovered-branch <commit-hash>
    

7. Undoing A Rebase w/ Reflog - It’s A Miracle!

If a rebase goes wrong or wasn’t what you intended, you can use the reflog to return to the pre-rebase state:

  1. Find the reflog entry before the rebase started:
    git reflog
    
  2. Reset your branch to that state:
    git reset --hard HEAD@{n}
    
    Replace n with the appropriate entry number from the reflog.

Exercise with “SampleProject”

Let’s simulate recovering from a mistaken branch deletion:

  1. Delete a branch (ensure it’s one you can afford to lose or is backed up): git branch -D experiment
  2. Use reflog to find the last commit on the deleted branch: git reflog
  3. Recover the branch by creating a new branch from the last commit: git checkout -b recovered-experiment <commit-hash>

By mastering reflogs, you can navigate and recover from many potentially disruptive scenarios, making it a valuable tool for safeguarding your work in Git.

For our “SampleProject,” enhancing efficiency with Git operations can be achieved through custom aliases. Git aliases allow you to create shortcuts for commands, reducing typing and making complex commands more accessible.

Writing Custom Git Aliases

1. The Global Git Config File

The global Git configuration file is located in your home directory (~/.gitconfig on Unix-like systems and C:\Users\YourName\.gitconfig on Windows). This file contains settings applicable to all your Git repositories, including user information and aliases.

2. Writing Our First Git Alias

To create an alias, you can directly edit the .gitconfig file or use the git config command. Let’s create an alias to shorten the status command:

git config --global alias.st status

Now, instead of typing git status, you can simply type git st.

3. Setting Aliases From The Command Line

You can add more aliases via the command line. For example, to create a shortcut for checking out branches:

git config --global alias.co checkout

This lets you switch branches with git co branch-name.

4. Aliases With Arguments

You can also create aliases that take arguments. For instance, to create an alias for adding and committing in one step:

git config --global alias.ac '!git add -A && git commit -m'

Use it like this: git ac "Your commit message". The ! at the beginning allows you to run any shell command, not just Git commands.

5. Exploring Existing Useful Aliases Online

Many developers share their favorite Git aliases online. Searching for “Git aliases” on websites like GitHub, StackOverflow, or personal blogs can yield a treasure trove of useful shortcuts. For example, you might find aliases for graphically visualizing the commit history, combining multiple commands into one, or simplifying the use of Git’s rebase and merge tools.

Exercise with “SampleProject”

Let’s set up a series of useful aliases for our project:

  1. Log Alias: To create a more readable log output:

    git config --global alias.lg "log --graph --oneline --decorate --all"
    

    Now, git lg provides a colorful and concise commit history.

  2. Squash Last Commits Alias: For squashing the last n commits (requires manual commit message entry):

    git config --global alias.squash '!git reset --soft HEAD~$1 && git commit'
    

    Use it like this: git squash 3 to squash the last three commits into one.

By customizing your Git experience with aliases, you can streamline your workflow, making it quicker and more enjoyable to manage your projects. Try creating aliases that fit your specific needs and workflow to maximize your productivity.

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home