Yes I know this has been done 1000 times, but I haven't done it and this is an exercise to help me remember certain things. Also, I hope people correct me and/or this helps others.
Note: Anywhere I mention "GitHub" below I really mean "remote repository". It's just that I feel most people that are new to this stuff will be searching for "GitHub
Checking in existing code to a new GitHub repository
Create the GitHub repo on their website from your account page. Be sure to add a .gitignore to exclude any directories or files you don't want to commit! In a prompt change directory to the root of your code folder then enter:
# initialize local repository git init # add all files to git tracking git add . # commit the tracked files to local repository git commit -m "initial commit" # connect to the remote repository git remote add origin https://github.com/blesh/some-project.git # sync remote and local git pull origin master # push files to remote git push origin master
Get a project from existing GitHub repository
Change directory to the directory you want to be the parent of your project directory. Do not create a new directory for your project, Git will do that for you.
# clone the master branch to initialize your project and local repo git clone https://github.com/blesh/some-project.git # change to that project directory cd some-project # [OPTIONAL] switch to the branch you care about git checkout somebranchname
Basic pull from GitHub
# usually you're on the master branch, # otherwise swap "master" for your branch name. gir pull origin master # sometimes you want to pull from a specific branch # so switch to that branch locally git checkout branchname # then pull it from the remote git pull origin branchname
Basic push up to GitHub
It's important to sync first with a pull!
# make sure you've added untracked files git add . # commit changes locally git commit -m "commit message here" # sync with remote git pull origin master # you might have to merge some changes at this point # if so, generally it will involve editing the actual files # and following the prompts in the console. # now push your changes up to the remote git push origin master
Overwriting all local changes with remote files from GitHub
So this one I've found will very depending on whether or not you have multiple branches you care about. This is also one where the difference between git pull and git fetch counts. For one thing, as of this writing git fetch as an --all flag you can use to pull down all remotes (branches and all), for another thing, pull will merge your files where fetch will not.
#get all files from all remotes without merging git fetch -all #reset your local to HEAD git reset -hard origin/master #remove all untracked files and directories git clean -f -d #[OPTIONAL] you might need reset and clean for other branches you've work on. git checkout otherbranch git reset -hard origin/otherbranch git clean -f -d
Updating a forked repository from the original
# add the upstream remote repository # in this case we're naming it "upstream", but you can name it whatever you choose. git remote add upstream https://github.com/angular/angular.js.git # fetch all files from all branches from the upstream repository git fetch upstream # switch to the branch you're working on git checkout master # rebase to replay all of the deltas from your previous commits on top # of the files you just pulled down with the fetch. git rebase upstream/master # push your changes back up to your own remote fork # the -f flag is to force the push and ignore checks and make sure # that the remote doesn't have issues reconciling which deltas to trust git push -f origin master