Setup SSH keys for use with GitHub/GitLab/BitBucket etc - README.md.
This tiny tutorial shows, how to generate a TOC for Markdown with the help of a tiny npm package called doctoc in just a few seconds. Since a while I write down the most important facts about a. Using your favorite editor, create a README file with the following content: Welcome to My First Repo - This repo is a practice repo I am using to learn Bitbucket. Save the README file in your bb101repo-practice directory. When you are done, list the contents of your local repository. See the companies that decided to use Bitbucket over Github or over GitLab and why developers are choosing one over the other. Review alternatives and overall pros and cons for each.
Learn how to undo changes on your local machine and a Bitbucket Cloud repository while collaborating with others.
Commands covered in this tutorial: git revert
, git reset
, git log
, and git status
Time | Audience | Prerequisites |
---|---|---|
40 minutes | This tutorial assumes familiarity with the following git clone , git commit , git pull , and git push |
Everyone makes mistakes. Not every push is perfect so this tutorial will help you use the most common git functions to undo a change or changes safely.
This tutorial assumes familiarity with the following git commands:
If you don't know those commands we can help you Learn git
with Bitbucket Cloud. Then come back here and learn how to undo changes. These git
commands are applicable to a windows or unix environment. This tutorial will utilize unix command line utilities when instructing file system navigation.
When the change you want to undo is on your local system and hasn't been pushed to a remote repository there are two primary ways to undo your change:
Command | Definition |
| An 'undo' command, though not a traditional undo operation. Instead of removing the commit, it figures out how to invert the changes in the commit, then appends a new commit with the inverse content. This prevents Git from losing history, which is important for the integrity of your revision history and for reliable collaboration. |
| A versatile
For a complete description of how |
As you progress through the tutorial you'll learn several other git
commands as part of learning how to undo changes, so let's get started.
Let's begin by creating a unique repository with all the code from the original. This process is called “forking a repository”. Forking is an extended git
process that is enabled when a shared repository is hosted with a 3rd party hosting service like Bitbucket.
Now that you've got a repository full of code and an existing history on your local system you're ready to begin undoing some changes.
You'll have to be able to find and reference the change you want to undo. This can be accomplished by browsing the commit UI on Bitbucket and there are a few command line utilities that can locate a specific change.
Git status
returns the state of your working directory (the location of the repository on your local system) and the staging area (where you prepare a set of changes to add to the project history) and will show any files which have changes and if those changes have been added to the staging area. Let us now execute git status
and examine the current state of the repository.
The output of git status
here shows us that everything is up-to-date with the remote master branch and there are no pending changes are waiting to be committed. In the next example we will make some edits to the repository and examine it in a pending changes state. This means you have changes to files in the repository on your local system that you haven't prepared (or staged) to be added to the project history.
To demonstrate this next example, first open the myquote2.html
file. Make some modifications to the contents of myquote2.html
, save and exit the file. Let us once again execute git status
to examine the repository in this state.
The output here shows that the repository has pending modifications to myquote2.html
. Good news! If the change you want to undo has, like the example above, not been added to the staging area yet you can just edit the file and keep going. Git only starts tracking a change when you add it to the staging area and then commit it to the project history.
Let us now “undo” the changes we have made to myquote2.html
. Because this is a simplified example with minimal changes, we have two available methods for undoing the changes. If we execute git checkout myquote2.html
The repository will restore myquote2.html
to the previously committed version. Alternatively, we can execute git reset --hard
which will revert the whole repository to the last commit.
The git log
command lets you list the project history, filter it, and search for specific changes. While git status
lets you inspect the working directory and the staging area, git log
only shows the committed history.
The same log of commited history can be found within the Bitbucket UI by accessing the “commits” view of a repository. The commits view for our demo repository can be found at: https://bitbucket.org/dans9190/tutorial-documentation-tests/commits/all. This view will have similar output to the git log
command line utility. It can be used to find and identify a commit to undo.
In the following example you can see several things in the history but each change is, at it's root, a commit so that's what we'll need to find and undo.
Let's look a little closer at one of the commits in the list:
What you can see is each commit message has four elements:
Element | Description |
Commit hash | An alphanumeric string (SHA-1 encoded) that identifies this specific change |
Author | The person who committed the change |
Date | The date the change was committed to the project |
Commit message | A text string that describes the change(s). Best practice tip: write short descriptive commit messages and you'll help create a more harmonious working repository for everyone. |
Most likely the change you want to undo will be somewhere further back in the project history which can be quite extensive. So let's learn a couple basic operations using git log
to find a specific change.
cd
(change directory) command.Enter the git log --oneline
command. Adding --oneline
will display each commit on a single line that allows you to see more history in your terminal.
Press the q key to exit the commit log and return to your command prompt at any time.
You should see something like the following example:
c5826da
and more changes in the list the git log
command produced. Someone didn't write a descriptive commit message so we'll have to figure out if that's got the changes we need.c5826da
from the git log
result in your terminal window.git show
then paste or transcribe the commit hash you copied and press enter. You should see something like this:The prompt at the bottom will continue to fill in until it shows the entire change. Press q to exit to your command prompt.
You can filter and adjust the output of the git log
with the following additions:
This filter | Does this | This example command | Would result in |
- | Limits the number of commits shown | git log -10 | The 10 most recent commits in the history |
| Limits the commits shown to the correlating time frame You can also use | git log --after 2017-07-04 | All commits after July 4, 2017 |
--author='name' | Lists all commits whose author matches the name | git log --author='Alana' | All commits made by any author with Alana in the name field |
--grep='message string' | Returns any commit with a commit message which matches the string you entered | git log --grep='HOT-' | All commits with HOT- as a text string in their messages |
This was a very brief look at the git log
command if you like working in the command like you'll probably want to check out the advanced git log
tutorial.
To get started let's just undo the latest commit in the history. In this case let's say you just enabled Bitbucket's CI/CD solution pipelines but realized the script isn't quite right.
git log --oneline
in your terminal window.log: 52f823c
then press q to exit the log.git reset --soft 52f823c
in your terminal window. The command should run in the background if successful. That's it, you've undone your first change. Now let's see the result of this action.git status
in your terminal window and you will see the commit was undone and is now an uncommitted change. It should look something like this:git log --oneline
in your terminal window. You should see something like this:HEAD
of the branch is commit 52f823c
which is exactly what you wanted.Let's say you've realized that pull request #6 (4801b87
), needed to be reworked and you want to keep a clean history so you'll reset the HEAD
to commit 1a6a403
this time you'll use the git reset
command.
git log --online
1a6a403
(myquote edited online with Bitbucket) which is the commit just below pull request #6 which has the changes we want to undo.git reset 1a6a403
in your terminal window. The output should look something like this:You can see that the changes are now in an uncommitted state. This means that now we've removed several changes from both the history of the project and the staging area.
Enter git status
in your terminal window. The output should look something like this:
git log --oneline
in your terminal window.The log output now shows the commit history has also been modified and begins at commit 1a6a403
. For the sake of demonstration and further example, Let’s say we want to now undo the reset we just did. After further consideration, maybe we wanted to keep the contents of pull request #6.
Git resets
are one of a few “undo” methods git
offers. Resets are generally considered an ‘unsafe’ option for undoing changes. Resets are fine when working locally on isolated code but become risky when shared with team members.
In order to share a branch that has been reset with a remote team a ‘forced push’ has to be executed. A ‘forced push’ is initiated by executing git push -f
. A forced push will destroy any history on the branch that was built after the point of the push.
An example of this ‘unsafe’ scenario is followed:
So far we have been passing git commit
Sha hashes to git reset
. The git log
output is now missing commits that we have reset. How will we get those commits back? Git never fully deletes commit unless it has become detached any pointers to it. Furthermore git
stores a separate log of all ref movement called “the reflog”. We can examine the reflog by executing git reflog
.
Your output from git reflog
should be similar to the above. You can see a history of actions on the repo. The top line is a reference to the reset we did to reset pull request #6. Let us now reset the reset to restore pull request #6. The second column of this reflog output indicates a ref pointer to a modification action take on the repo. Here HEAD@{0}
is a reference to the reset command we previously executed. We do not want to reply that reset command so we will restore the repo to HEAD@{1}.
Let us now examine the repos commit history with git log --oneline
:
Here we can see that the repo’s commit history has been restored to the previous version we were experimenting with. We can see that commit 4801b87
restored even though it appeared lost from the first reset operation. The git reflog
is a powerful tool for undoing changes in the repository. Learn more in depth usage on the git reflog
page.
The previous set of examples did some serious time traveling undo operations using git reset
and git reflog
. Git contains another ‘undo’ utility which is often considered ‘safer’ than reseting. Reverting creates new commits which contain an inverse of the specified commits changes. These revert commits can then be safely pushed to remote repositories to share with other developers.
The following section will demonstrate git revert
usage. Let us continue with our example from the previous section. To start let us examine the log and find a commit to revert.
For this example let’s pick the most recent commit 1f08a70
as our commit to operate on. For this scenario let's say that we want to undo the edits made in that commit. Execute:
This will kick off a git merge
workflow. Git will create a new commit thats content is a reverse of the commit that was specified for the revert. Git will then open up a configured text editor to prompt for a new commit message. Reverts are considered the safer undo option because of this commit workflow. The creation of revert commits leave a clear trail in the commit history of when an undo operation was executed.
Congratulations, you’re done! Come back to this tutorial any time or head to the Undoing Changes section to go more in depth. Keep up the good work in Bitbucket!