- Hi, I'm Cameron McKenzie.
I'm the editor in chief over at theserverside.com.
And I wanted to talk to you a little bit
about git, specifically git reset
and what happens when you do a git reset
and then push to a remote GitHub repository.
So I wrote an article a little while ago
about doing a git reset hard,
and what people kept asking me was, well,
what happens to the commits that you reset over
when you actually push to something like GitHub or GitLab?
So I want to demonstrate exactly what happens
in this tutorial.
So I've got a GitHub repository in which I'm just going to
create a very basic repository.
So here I am, my repository, just going to create a new one.
And I'm going to call it git reset explained.
And I'm just going to put a read me file.
I don't know, what, Java git ignore in here.
And maybe even an MIT license and create the repository.
That now creates a remote repository
and it's got a commit on it.
You can see the commit ID, 7B1, blah blah blah.
But now I've got a basic git repository on GitHub,
a remote repository.
And I would like to now do some commits locally
and then push them back up.
Maybe even include a reset.
So I got a folder here called git reset hard.
I'm going to go into the Bash shell.
I'm just going to re, I'm just going to clone
that remote repository.
Maybe even just increase the font a little bit here.
There we go.
That looks a little bit more handsome.
And from here, I'm just gonna
clone that remote repository.
Git reset explained.
Hey, if I do a nice little ls command,
it seems like, yeah, I've got that repository.
Now I'm just going to move into that repository.
I am now there.
And I think at this point in time,
I can just go ahead and now create a bunch of files.
Now this is going to take a second, you know,
create one file called alpha.HTML,
and then I'm going to commit that.
I have to do a git add first, and then a git commit.
Add a message in there and say local commit number one.
And I'm going to do this five times
just to create a nice little history
of five resources.
So now I'll touch beta.
Do commit number two.
Add in charlie.
Do commit number three.
Throw in Depeche Mode.
Do commit four.
And now of course, I'm going to throw Enid in here.
She never knew me anyways.
And then throw in that fifth commit.
And so now when I do a git reflog,
I should see a nice rich history.
I've got my five commits, one, two, three, four, five,
and I've also got that other commit there, seven one.
That's actually the one from GitHub.
So if I was actually go back to that GitHub repository,
you'd see that git commit right there.
Okay, so now I've got my rich history.
Well, what's gonna happen if I go and reset the head
to that third commit?
I'm gonna do a hard reset, git reset hard.
And I'm going to say, let's reset to that third commit,
which is 06DF67A.
So the head's now pushed back.
This is what people tend to do if they want to
kind of eliminate or delete a commit
or kind of go back to an earlier place in history.
But you notice that if I do it a git reflog,
all of a sudden my head is pointing to this third commit,
067, you can see that's the same number as right here.
If I do an ls command,
you'll see that I've got the three files,
alpha beta charlie.
So we have deleted the enid and depeche files,
but those commits are still in my history.
Those commits are still there.
And a lot of times we need to reset.
What you're trying to do is you're trying
to delete the commit history or get rid of it.
So definitely it looks like it's still hanging around
in my local repository.
And in fact, that's not too big a surprise
because we know that when we work with git,
git likes to maintain everything.
But what would happen if we go over to GitHub
and push these changes up?
Okay, so we've got one commit there right now.
If I now do a push for my local repository,
will I add five commits
and have a total of six commits there,
or will the two commits that we reset over,
get leapfrogged over and not be pushed up,
providing only four commits?
That's my first three commits, plus the commit
that happened on the server.
Let's see, let's do a git push origin.
It's going to ask me for my username.
And then this little window is going to come up
asking for the password.
I'll type that in.
The push goes to the server.
I thought that said refuse, but it says like it's reused.
That's much better than refuse.
And if we now look at the commit history,
you notice that now we've actually got four commits.
And that's interesting because when we do the git reflog,
you'll see that there's much more commits
in our local history.
And so the GitHub server only has the initial commit.
That's the one that the server made
when we added the read me file.
And it also only has the three local commits.
Commit one, commit two, and commit three,
which added those files in it, alpha, beta, and charlie,
despite the fact that we do have two other commits
on our local system.
So there you go.
When you do a git reset, indeed,
we will leapfrog over the various commits that are local,
but those commits don't get deleted
or orphaned from our local repository.
But when we do a push,
git ignores them and git does not push them
or publish them to remote servers.
And there you go.
That's how a git...
That's how a git reset and a push to a remote GitHub
or GitLab server works.