- GitHub Training
- Webcast - Undo, Redo & Rebase Your Git History - May 2014
Click on text below to jump to specific point in the video
My name is Jordan McCullough and I am part of the GitHub training team. Lets get things started by creating a repository.Go to account and create a brand new repository. I am going to call this a free class. Provide a description The other thing is to keep this repo public. Initialize with README. Apply a license. Update the README title. Add a subtitle. Commit with commit message. On the GitHub platform, any text file that ends with .md uses MarkDown format. Add a couple of edits to generate some history.
bullet list of topics to be reviewed.
Now I am ready to bring it down locally. Easiest way is to use "Clone in Desktop" button. Specify a path to save the repo on local. When I performed a clone, GitHub's client made a copy of everything going on in the project. The commits, the history, all versions of the file which live in this. I can confirm that by taking a look at the history in the GitHub client.
I have these local repos, the one of interest being the free class. I want to show this to you in the Finder. I go to terminal and do some stuff with git.Change directory to a git repo project.
git statusConfirms the repo and if I have any existing modifications.
I edit the readme.md file.I will save the file with the modifications.
git diffGit shows what is missing.
git add .Stage the file.
git commit -m 'Remove extra content'Commit the file.
git commit --amend -m "Remove extra text and divider line"Revise the last commit.
Now I introduce some other change. But I don't want the change. I can recover it in a couple of ways.
git log --onelineWill show same commit history as on graphical client but just on the terminal instead. Hit Q to leave or drop out of this. Notice that commits are identified by commit refs - hexadecimal values. I wish the last commit never happened at all. Let me identify the place to go back to - where I would like to recover to. Copy the commit ref.
git reset commitidReset's the history. I have now thrown away the commits.
Say I did not want to lose the changes, I just wanted to destroy the commits themselves.
The changes now appear in the graphical client as if they had just occurred. And as if I have never versioned those modifications.
git log --all --decorate --onelineI want to see what history is on GitHub. And what history do I have just local and specific to my machine keeping in mind git is distributed in nature, and that unless we tell git to communicate with a remote repository (github.com in this case) these commits are only sitting on hard drive. Also these commit refs are not the most friendly thing to refer to - they are verbose. Let us discard any commit made locally.
git reset --hard origin/masterReset back to branch that represents the last time we communicated with github for this repository.
Maybe you accidentally ran the hard reset and lost more changes than you wanted.
git refloggit keeps a history of your interaction with the repo for your local copy only. This is never transmitted to the repo on github.com. I can see the last 7 commits, the 7 interactions with my repo locally. Even though my git log doesn't show all those other commits, git has actually stored them in the background for us. We can get back to them if we really need to recover.
I am going to generate some random changes. I would like to bundle them into one commit.
git reset origin/master
Now you see the 5 files changed. Now you can commit all at once. Now I will publish it to github.
You shouldn't have committed those files - don't reset any history which has been shared with others. Other developers may rely upon this commit. There is a unspoken rule, once you publish the repo, you never use the reset command on that commit.
How would I create a new commit which removes them?
git revert commitrefWhy is that bad? Revert creates a new commit marker which simply does the inverse of what happened. In my file system I don't have the files in my working tree.
I will bring up reflog again.
Imagine we want 7 more commits. They are not on GitHub yet.
git rebase -i origin/masterYou need to revise what happened to a safe point in time. I want to go back to a point in which I do not override or rewrite history that has been shared with others. Rebase shows oldest at top and newest at bottom.
Maybe I don't want any commits that have even numbered files. I will delete the lines representing even numbered files. I save that. It says rebase updated master. If you rebase interactive again, now my history since I last interacted with github.com - I only have commits with odd numbered files.
I don't want them as individual commits - I want one commit. We can achieve this using the letter s (for squash).
git config --global alias.lol "log --graph --all --decorate --oneline"This creates an alias command called lol for command specified in quotes globally.
git lol --statAdditional commands can be supplied after the alias.
What is the difference between reset origin master and reset hard origin master?reset origin/master is saying throw away all commits but retain the files that were introduced. reset hard - you don't care about what the changes are, what you are rewinding to. You don't even care to keep what's modified. It is a stringent command and wipes everything clean.
Can you only revert head commit?It is good to be careful what you are reverting. Revert is the safest way you can undo a change and continue to have history moving forward. I can grab a specific commitref and revert to that.
Would you show us how to commit a single change through the command line?git commit -m "message"Add a single commit.
Thank you.Video outline created using VideoJots