Git: Undo a rebase with git reflog
If you make a mistake whilst rebasing, it might seem hard to undo. But with git reflog, you can find the original commit SHA and revert back to it. Let’s see how with an example.
(All tags.)
git reflogIf you make a mistake whilst rebasing, it might seem hard to undo. But with git reflog, you can find the original commit SHA and revert back to it. Let’s see how with an example.
Earlier this week, I made another podcast appearance on The Python Show, episode 22: Git and Django with Adam Johnson. As fellow authors, Mike and I talked a lot about the writing process, on topics like:
Last week, I made another podcast appearance on the PyBites Podcast, episode 139: Maximizing Your Developer Experience (DX) with Adam Johnson. We talked about various topics, including:
I had the pleasure of returning to The Real Python podcast in last week’s episode 179, Improving Your Git Developer Experience in Python. It was great to catch up with host Christopher Bailey and chat about:
git log with a commit SHA shows that commit and those before it:
--force-with-lease and --force-if-includesWhen you push, Git checks that you are only adding commits to the remote branch. If you try to push an out-of-date branch, it will fail:
To show only the subject of the latest commit (the first line of its message), use:
You worked on a sizeable feature branch some time ago but left it to focus on other things. Now you’re ready to pick it up, so you want to rebase it onto your updated main branch. But when you try that, you’re presented with an overwhelming number of merge conflicts.
I released Boost Your Git DX nearly two weeks ago. It’s the second in my “Boost Your DX” series, following last year’s Boost Your Django DX. Both books aim to improve your development experience with their respective tool.
My new book, Boost Your Git DX is out now, on my birthday 🥳. It’s taken nearly a year of work with four months in early access.
To show the initial, or root, commit* of your repository, use:
I’ve once more had the pleasure of joining Carlton and Will on the Django Chat podcast, in Episode #146. We spoke on Tuesday, just hours after Django 5.0 alpha 1 was released, on several topics:
.gitkeep files, use .gitignore insteadGit only tracks files, not directories. It will only create a directory if it contains a tracked file. But sometimes you need to “track” a directory, to ensure it exists for fresh clones of a repository. For example, you might need an output directory called build.
.git directoryEvery object you commit in Git is copied into its object store, within the .git directory. If you undo a commit, the commit object and associated file objects remain in Git’s object store, at least for a while. The normal garbage collection process will clean them out, by default in 30 days.
To output the root directory of the current repository, use:
To print just the name of the current branch, use:
I’m happy to announce that my new book, Boost Your Git DX is available now, in “early access”.
To detect if a certain operation is in progress in a Git repository, use git rev-parse like so:
Git hooks are useful for enforcing standards in your repositories. But sometimes, they’re in the way and you gotta skip ’em (locally), such as when writing “WIP” commits, experiments, and even testing hooks. Here are a few techniques for skipping hooks.
POSIX permissions include the execute permission, which allows the file to be executed as a program. The permission can be set for each of the three classes: user, group, and others. So, for example, you can have a file that is executable only by its owning user.
Different filesystems have different case sensitivity, for example:
Many Git commands output “advice”, with hints about which commands you could run next. Most notably, git status gives you advice for what to do about files in each state:
There you are, minding your own business, rebasing your branch and… surprise! A merge conflict!
pre-commit uses Git’s hook system to run tools when you commit. Unfortunately, Git doesn’t run any hooks when making a commit during a rebase. This can lead to you rebasing a branch and not realizing some code needs fixing, at least not until your CI system runs pre-commit (say, with pre-commit.ci).
Have you ever accidentally committed a bunch of junk created by your OS, like Thumbs.db files from Windows, or .DS_Store files from macOS? Or, have you joined a project, and for one of your first commits, added rules to the .gitignore file for your text editor’s project files? If so, this post is for you! You can avoid such pain or busywork by making a global ignore file.