How to Push Code to a GitHub Repository

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you know there's two ways to get an existing project into a GitHub repository there's an easy way and there's the proper way I want to show you both ways to do it and I'm pretty sure I can make a compelling argument that says the easy way is actually the better way in a number of circumstances hi I'm Cameron McKenzie I'm the editor-in-chief over at theserverside.com and last year I wrote an article about moving an existing project into a GitHub repo and I talked about the proper way to do it but I felt guilty because I knew all along that there was actually a much easier way to do it and I want to show you that easy way as you can see I've got a big project on my file system you can see Gradle and Maven and Python and package.json files for JavaScript and node applications and I want to get that big project up to a repository in GitHub I've also got my GitHub account open here and I'm going to create a brand new repository and I'm going to call it push it easily gonna make it public and I'm going to add a readme file this is pivotal this easy cheat only works if there's some sort of a git commit history in your repository so make sure you add that readme file you can add anything else that you want I don't care but this actually simulates a situation where you're adding a project to a repository that already has files in it the proper way assumes there's no files in it so this is actually probably closer to a real world situation than the proper way to do it now you'll notice there's a beautiful green button over there and I can't help press a beautiful green button and that gives me my get URL I then need to find somewhere to clone this repository and I got a folder called repos and that just seems like a smart place to clone a repository so I'm going to do that git clone command I'm going to paste in that URL looks like it didn't get copied properly so I'm going to do that again I'm going to work on my copy and paste skills I got to take that off my LinkedIn resume until I really nail that down but you can see the push it easily folder created that's got the dot get folder in it which shows you that there's a git repository there's the binary embodiment of git of your branches of your remotes right there and now the question is how do I get that big project into this git repo well can you copy and paste Ctrl a control C just copy all of those files maybe make sure that you're copying hidden files too I don't know if you've got hidden files in your project it seems weird to me but people do weird stuff and then just paste it in can you do that can you paste your files into your git repository now what do you do well you do what you normally do you just go into that folder so CD push star and then get add Dot get commit Dash M adding project to GitHub repo and then you know where this is going get push origin done boom all of our files have now been pushed up to GitHub if I go over here do a refresh boom main SRC test Java Gradle build files mavenpom files reactant angular package.json files python.py files I bet if I go into this SRC folder I might even see some crazy Ruby files there it is in that crazy main folder so I mean you tell me does it get any easier than that now let's see what the proper academic way to add files to a repository is so let's now create a brand new repository I'm going to delete that folder there delete that get bash shell I'm going to create a brand new repository now this repository is going to be totally blank and it's going to be called push it properly I'm always pushing it there we go push it properly it's public but fight every temptation to add a readme or get ignore or a license or anything like this this has to be a naked repository as unchristian as that sounds so we create the repository and look it even says look okay you created a repository if you actually want to add a project or connect to your from your local machine to this you got to do get in it get ad get commit get Branch get remote get push Dash you originally it's nasty but just because it's nasty doesn't mean we're not going to do it so let's do it the proper way I've gone into my big project I'm going to open the get bash shell there and then I'm going to issue that get i n i t command get init better than getting out of it get in it we see the dot get folder created there now what is next it says do a git add so git add dot it says do a git add readme because I think it wants to create a readme file first or something like that I don't know but we do the get at that now stages all the files now we need to do a git commit get commit Dash M add to remote GitHub adding this project to a remote GitHub all our files are committed we're not there yet we can't push right because we've got to add our remote origin okay so here's this command git remote add origin https blah blah blah blah blah now that connects our local repository to the remote GitHub repository in terms of of allowing our local repository to know where to push to but no files have been pushed up yet so the next step is to actually push up now the other problem here is notice I'm on the master Branch when I did the get in it it created a master Branch not Main and the instructions actually say to work with the main branch so like it's it it's just more things to mess people up I've actually seen on the message boards lots of people saying they can't move a project into GitHub and they're getting errors because they use Master instead of Main we're on the master Branch I'm going to do a git push origin git push origin got to do a dash u in there Dash U Dash me Dash origin and then what branch are we pushing we're on Masters we're going to push Master up not Main laughs okay which one was easier I don't know I'd click refresh and the files are here so it worked but that wasn't easier it seemed like a lot more work and it's also problematic okay and I'm gonna I'm gonna show you how it can become problematic I'm going to close this window and I'm going to delete that dot get folder that's essentially now removing my local project from from git the git and the linking to the server is no longer happening so I'm also going to delete this uh repository up on the server as well so I can start off with a brand new clean repo with nothing in it getting back to that nakedness that they talk about in the Bible and here we go uh now we've got uh We've deleted that repository now let's create it again so push it properly it's going to be public now I'm going to add a readme here so now things are a little bit different and I think this is simulating what more people run into where they want to add files to a project that may already have files in it so I'm adding that readme I'm creating the Repository I'm going to click that beautiful green button again because I just can't fight the Temptation we're going to need that in a moment and then we're going to do those basic steps again so over here I'm going to go into my big project I deleted that git folder so I need to do that get in it again get in it get out of it okay you see that dot get folder magically appear now we need to do the git add dots that's stages all of the files now we need to do a git commit git commit Dash m uh project to existing GitHub repo the commit is done now what do we need to do now we need to add the remote so git remote add origin and paste in that URL now notice we're on the master Branch here and the server only has a main branch so I'm gonna have to create a master Branch over here despite the fact that I'd prefer not to but we're kind of boxed into this um but now at least we've if we're on the master Branch locally we want to push to the master branch on the server that's going to exist and that's again that's probably more the case where you're copying files into a branch that exists so now we've got that working here and now according to the instructions I just have to do git push Dash U origin master and if I do this get push Dash U origin Master Watch What Happens it actually rejects the push it actually says look you've got a different commit history locally than you do on the server and we can't resolve that we don't know about this commit that you have and then this commit that we have and then all of this weirdness that you're trying to push into so you end up getting a rejection here and nobody likes rejection the only thing we can do is force ourselves on it and again forcing ourselves on it just again sounds just doesn't sound right to be forcing yourself on anybody but we got to do it we got to do a git push Dash U Dash f to force to origin and force our Master Branch onto the server there and now this runs and it works and now we go over to GitHub and do a little bit of a refresh we can see that our files have gone up there but again I don't like it uh I don't like having to do that Force I worry about that Divergent get commit history between the two I think just doing the Clone copying your files over doing a git ad doing a git commit and then doing a get push just I think it just makes everything a lot easier to work with now your mileage may vary on that but I think that is a much better way of doing it so again please uh let me know if I'm missing something here like uh let me know if there's a step that I'm missing or this causes some problems but I think this actually solves a lot of problems taking the easy approach um and so there you go now before I let you go I just want to say you know I'm kind of well known on YouTube for creating short tutorials that tackle a very specific or difficult problem with Git so the leading repositories doing merges fighting with uh merge conflicts doing git flow like all of those things very short tutorials but I've kind of branched off into creating some longer form tutorials so if you're interested I've got a bunch of tutorials on things like how to use GitHub desktop and I go from start to finish on GitHub desktop talking about branching talking about collaborative work with pushes and polls talking about doing rebases talking about all of the stuff that you can do with GitHub desktop I've done something similar with sourcetree and bitbucket and gitlab so I just want to encourage you go check out those long form tutorials there might be something in there that you might find valuable and also if you enjoy my tutorials wanted to uh suggest them to some of your friends too I'd like to see a little bit more traffic on the site one of the funny things is with the short form tutorials get downgrades them so git can only put an ad into a video not kit YouTube can only put an ad into a video after the eight minute Mark so they don't like promoting videos that are longer than eight minutes long that's why you can get some really garbage uh nine minute videos that don't tell you anything and you don't you have to really hunt for the short two minute videos that tell you exactly what you want um so I'm trying to play with that YouTube algorithm a little bit and creating some more long-form stuff so please check that out oh and uh one other thing before we go you may notice that some of the books just disappeared from the background there um that's because I just wanted to suggest them I don't know if you know this or not but Springfield in The Simpsons is actually based on a small town in Toronto named Pickering uh just outside of Toronto I wrote a book called Pickering is Springfield that's available on Amazon I would definitely suggest you check that out um I also got a book on hibernate and jpa a couple of other books on Java programming too but if you're in the Java space please check those out now the other thing too is if you're talking about git you're doing development if you're doing development you're most likely agile and if you're agile you're most likely using spring and one of the Freelancers that's been doing a lot of work for us over on the server side named Darcy declut scrumptuous on Twitter uh she wrote this scrum Master certification guide can't recommend it enough please go check that out so if you're interested in agile you're interested in scrum you want to learn more about scrum even Advance your career as I said that's a great resource to pick up um also you know I started to tweet a little bit more on X uh Russell rights not Twitter anymore on X so if you're on X um look me up at cameronmcnz and and follow along all of my personal Antics I am the editor-in-chief at the serverside.com so we got lots of tutorials on git GitHub sourcetree devops tortoise get get Kraken um Maven Java python you name it and of course if you're watching this on YouTube why don't you subscribe on YouTube maybe give a like but definitely subscribe it'd be appreciated
Info
Channel: Cameron McKenzie
Views: 40,265
Rating: undefined out of 5
Keywords: cameronmcnz, theserverside, java, jakarta, jakartaee, git, github, gitlab, dvcs, versioncontrol, tortoisegit, gitgui, atlassian, bitbucket, linux, azure, devops, devopstools, gitops, gitopstools, azuredevops, sourcetree
Id: ueQs5pQ8ZMM
Channel Id: undefined
Length: 15min 29sec (929 seconds)
Published: Tue Aug 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.