23
loading...
This website collects cookies to deliver better user experience
git log
command against our repo, they get an accurate idea of how the project has been evolving lately.git rebase
comes in! Let’s get started; make a directory called rebase_and_bisect
, cd
into it, and run the git init
command. If you’re using a Linux or Mac terminal, or GitBash for Windows, the following commands will do this:$> mkdir rebase_and_bisect
$> cd rebase_and_bisect
$> git init
poem.txt
and commit it to the repository:$> touch poem.txt
$> git add .
$> git commit -m "Create file"
$>
) so that you can copy and paste them into your terminal, saving you some time. (Please note, the following commands will probably not work with PowerShell or CMD, please use GitBash if you’re on Windows):echo "Roses are red" > poem.txt
git commit -am "Add first line"
echo "JavaScript is yellow" >> poem.txt
git commit -am "Add second line"
echo "I'm learning git" >> poem.txt
git commit -am "Add third line"
echo "and I wanna say hello!" >> poem.txt
git commit -am "Add fourth line"
poem.txt
.$> git config --global core.editor "nano -w"
nano -w
. For a more exhaustive list of how to set specific text editors, click this link.rebase
.git log
, they see all the commit messages made on the project. Our goal is to write concise yet descriptive messages of the work we’ve done so that the collection of commit messages show an accurate picture of where our project came from and how it’s evolving.git rebase
, we can rewrite history!git rebase
is rewriting commit messages. We’ll open up poem.txt
in our text editor of choice and sign our name at the bottom. Then we’ll commit the changes but, oh no! We make a typo:$> git commit -am "Signing my nammee"
git log
to see the typo in our list of otherwise good commit messages. We’re now going to use rebase
to fix this. We’re going to use the interactive version of rebase to do this, and we’re going to go back one commit when rebasing. So the command I want you to run is:$> git rebase <strong>-i</strong> HEAD<strong>~1</strong>
-i
is for “interactive” and the HEAD~1
means “go back one commit from the latest version.” As soon as you hit enter, you should see something like this:pick
and type reword
. Then, save the file and exit it (if you’re using nano, you save by pressing “Ctrl + o” then press enter and exit with “Ctrl + x”).#
s) are all tildes (~
). In my GitBash, vim looks like this:Esc
key. Once you’ve done this, you can save the file by first pressing the key combination for colon (:
) which is Shift + ;. You’ll notice now that your cursor is now at the bottom left of the screen. Type wq!
and then press enter. This is the command for saving and exiting (or writing and quitting). Repeat the process when it opens the commit message file and you should be good to go!poem.txt
file. I open it up, delete my name, save, the file, then run:$> git commit -am "Removed name"
$> git commit -am "Finish removing name"
rebase
, this time going back two commits, so I run:$> git rebase -i HEAD~2
git-rebase-todo
file opens, I have the last two commits listed. This means I want to squash the most recent commit into the last one, so I change pick
to squash
on the second line:rebase
and I encourage you to read the commented lines detailing the commands in the git-rebase-todo
file. Play around with it some to get a feel for what you can do with this useful tool.bisect
command is pretty cool because it helps us locate the source of a bug by searching for the specific commit a bug was introduced. The way it works is we first identify a “bad” commit, or a commit in which a bug exists, then we identify a commit in which the bug does not exist, or a “good” commit. Then, git will checkout old commits between the good and bad ones and ask us if the bug still exists.bisect
tool. Run the following commands:echo "This poem is about JavaScript" >> poem.txt
git commit -am "Add description of poem"
sed -i -e 's/yellow/orange/g' poem.txt
git commit -am "Replace 'yellow' with 'orange'"
echo "This is for my git tutorial" >> poem.txt
git commit -am "Add reason for poem"
poem.txt
and note the bug, “yellow” seems to have been replaced with “orange.” See for yourself:$> cat poem.txt
Roses are red
JavaScript is <strong>orange</strong>
I'm learning git
and I wanna say hello!
This poem is about JavaScript
This is for my git tutorial
bisect
to identify the commit that introduced this bug. The first thing we have to do is identify a good commit–where the bug doesn’t exist–and a bad commit–where the bug does exist. Run git log
to find these commits. Please note that your commit hashes will not match mine, so for the rest of the article, you cannot copy/paste my example commands, you’ll have to use your relevant commit hashes.start
the bisect wizard by running git bisect start
, then identify the good and bad commits with git bisect good [hash]
and git bisect bad [hash]
. Take a look:$> cat poem.txt
Roses are red
JavaScript is yellow
I'm learning git
and I wanna say hello!
This poem is about JavaScript
git bisect good
. Git then picks another commit between this one and the commit we marked as bad. Take a look:git bisect reset
which returns us back to the most recent commit. Now that we know which commit introduced the “yellow -> orange” bug, we’ll use rebase
to get rid of it.git diff
command and passing in two hashes.d5ebcc3
. If I want to reference the commit before that without looking up its hash, I can append ~1
to the hash as something of a relative way of referencing the previous commit. In other words, if I want to compare d5ebcc3
and the commit prior to it with diff
, I would run the following command:$> git diff d5ebcc3<strong>~1</strong> d5ebcc3
$> git rebase -i d5ebcc3~1
git-rebase-todo
file we saw earlier. This time, I’m going to drop
the offending commit. Not only will that remove that commit’s message from the project’s history, it’ll also get rid of the changes made in that commit. Make your file look like mine (but don’t change the commit hashes):$> cat poem.txt
Roses are red
JavaScript is yellow
I'm learning git
and I wanna say hello!
This poem is about JavaScript
This is for my git tutorial
git log
and note that the offending commit has been stricken from the record:rebase
to keep our history clean, and bisect
to help us track down bugs. That concludes the Professional Version Control with Git series, but keep an eye out for more content about version control and collaboration tools. Don’t hesitate to contact me with any questions you have by emailing [email protected] or hitting me up on Twitter, @ErikWhiting4. Thanks, and see you next time!