Check out a free preview of the full Everything You'll Need to Know About Git course

The "Push" Lesson is part of the full, Everything You'll Need to Know About Git course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen explains how to push changes from a local branch to a remote repository using the "git push" command and demonstrates the process by making changes to a branch called "bar" and pushing those changes to a remote repository called "hello git". He also discusses tracking branches, the importance of properly naming remotes, and briefly mentions alternative strategies for handling changes when pulling from a remote repository, such as stashing or using work trees.

Preview
Close

Transcript from the "Push" Lesson

[00:00:00]
>> So what's the opposite of pull? Well, push, right? Awesome. So if you wish to take your changes and move them to a remote repo, you can do this with git push. If you're not tracking, git push won't work just like git pull wouldn't work. So you have to be able to be tracking a branch on a remote.

[00:00:15]
Again, if you properly name it as origin, git has a magic around it where you check it out, it'll do some auto tracking for you, which is fantastic. So let's make some changes to bar and push it to hello git. So I'm gonna go here in remote git and I'm gonna go git checkout bar.

[00:00:33]
Now notice, when I git checkout, look what it does. It automatically does the tracking for me. Awesome, so now I can just git push. And notice that I didn't have to create a new branch, I already had this branch in my remotes directory. If you have several remotes, it can cause some problems and all of sudden checking out can be a little bit more of a pain depending on how you name things.

[00:00:54]
That's why the whole git checkout me bar I thought was funny, but it also can be a pain. Anyways, so, we've now switched to bar, we'll do a little quick change. Let me just make sure that this is how we're gonna do this. Let's see. Create a single commit, CHANGE FROM REMOTE, there we go.

[00:01:08]
And we're gonna add it to the end of README. So everyone do that, add one line to the end of READBAR in bar. So I'm gonna go vim README, go down here, changes from remote, git status. Yeah, there's only one file changed, git add, git commit, changes from remote, there we go.

[00:01:32]
So now I can take this and I can push it to my remote. I can go git push, and look at that, we've just updated back bar in hello git. If I switch over to hello git and go get log bar --oneline, you'll see this right here. It's been updated for us, cuz I pushed and it changed it on the remote.

[00:01:57]
So this should be pretty familiar to you, because it's like you already do this with git hub a bunch, so none of this should be a surprise. Probably, the only real surprising part is the fact that it's just two folders on your computer, right? Again, folders, directories, I know, windows, played Doom, loved Doom, Doom was a great game.

[00:02:13]
So we pushed all the changes, we've logged it out, we can see all the changes, a change from remote, so that's pretty awesome, right? Now, you may be asking or questioning this, why was bar moved forward for us? We fetched the results, it didn't move our branches forward, but why wouldn't we push it to do it?

[00:02:36]
Well, we're not checked out, right? That branch isn't checked out. We're not using that branch in any sort of capacity and we're doing a push, which is analogous to doing like a fetch and a merge. We're just doing it for them. And so that's what's happening on the other side, it's fetching, it's merging, it's updating its reference for bar, and there we go.

[00:02:56]
It's like the opposite of pull. I'm gonna every now and then say the word upstream instead of origin, upstream, that is just by accident, old habits kind of die hard. When I say upstream I mean hello-git. So, when I say origin I mean hello-git, cuz remember, we have a dependency of remote get on hello-git, but hello-git doesn't have its remote setup to remote, right?

[00:03:20]
So it's a one-way relationship right now. So we're just gonna pretend hello-git is github, for this case.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now