Skip to content

Instantly share code, notes, and snippets.

@raykao
Last active December 17, 2015 02:18
Show Gist options
  • Save raykao/5534448 to your computer and use it in GitHub Desktop.
Save raykao/5534448 to your computer and use it in GitHub Desktop.

Basic Git/GitHub Commands:

Creating an initial repository

Assuming you've created a directory called project_dir and all your code/files/folders/assets etc are in this folder then you can initialize your git repo by running the following in your $ shell/terminal prompt:

project_dir$ git init

Adding file changes to the Repo

Now you can write your code, add files and folders etc. to your project_dir. Once you've done this and want to start saving your changes to the git repo, you can:

Detect all file additions/changes/deletions

project_dir$ git add -A

The -A is important as it notifies git add to pick up new/changed files AND removes any files that have been deleted through other means (i.e. not with git rm <filename>.

This command only registers/recognizes you've made changes but does not commit any of this yet to git.

Remember that you must run this each time you're ready to register you've made changes. Note that git add -A is not a constantly running process/daemon picking up your changes. You need to manually ask git to do this.

Committing changes to the repo

project_dir$ git commit -m "place your commit message here e.g. Initial Commit"

This now stores the file changes you've made to a commit head saving it in a "snapshot" in time.

It should be noted that this is only a differential change (additive/subtractive) from the previous head/base file setup. Most efficient use of storage, rather than a complete copy of the files for each commit.

Pushing changes to a remote repo/server

#in general:
project_dir$ git push <remoteName> <branch>

# example
project_dir$ git push origin master

# origin is usually the default <remoteName> and <branch> can be assumed as the one you're on; command can be simplified to just:
project_dir$ git push 

# if you've created a new branch locally but haven't created it on the remote server
# you can push and create a new branch to the remote server in one command
project_dir$ git push -u <remoteName> <newBranchName>

Adding a remote 'upstream' git repo/server to push to

In short the command to do it is:

# Your dev/testing environment
project_dir$ git add remote <remoteName> <userName>@exampleServer.com:<folder>/<remoteRepo>.git

# Example with GitHub
project_dir$ git add remote origin git@github.com:userFolder/remoteRepo.git

This will create an 'upstream' or remote repo/server to save your code to. Services like GitHub and Bitbucket provide this service for you for a pretty damn low price. But you might feel adventurous...or cheap...and want to host and maintain your own git server. If so checkout the next section Creating your own self hosted remote git repository/server

Command Summary

# in general all you need is:

$ git add -A
$ git commit -m "commit message - try to be clear and useful"
# if you've already got an upstream/remote git server to upload/push to:
$ git push

# if you dont have a an upstream/remote git server to upload/push to - do this first AND only once:
$ git add remote origin git@serverName.com:folder/remoteRepo.git
# then you will push the 'long' way for the first time:
$ git push origin branchName

Creating your own self hosted remote git repository/server

This can be a tricky step and there are a few ways of doing this.

Self Hosted git Server/Repository Setup

I'm going to assume you're runing a Debian based linux server (i.e. Ubuntu, Mint...) and know how to remote into it via command line.

Some commands used here will be specific to this type of linux, but you can run whatever you like, as there is an equivalent command for each flavour of linux distribution - it will just be a small pain/hurdle for you to Google it.

I'm going to also assume you know how to connect to the remote server via the ssh command, so that you can run commands and install/run stuff on your server. If not, I'd refer you to a basic linux command tutorial/book.

Commands

# Remote Server

$ sudo apt-get install build-essential checkinstall

$ sudo apt-get install git-core

$ sudo adduser git

Explained

Here we connected to our remote server and:

  1. Installed build-essential

    • build-essential is a set of packaged goodies that you'll need to compile/install software packages
    • It's a must have but for whatever reason it's just not shipped by default 99.999% of the time...go figure
    • google build-essential or g++ compiler for more info why they hell you need something like it
  2. Installed git-core

    • this is a package that will download the code for git, compile it and then install it for use.
    • Note: If you skipped step #1 and you don't have build-essentails or something equivalent already installed , this step would have failed miserably
  3. Created a new user account "git"

    • creates a new account that will be used to store your git repos
    • you could have named this new account anything, but it's simpler, more organized and just makes sense to name it "git" dontcha think?
    • keeps the files organized and separate from your server user account/home directory (you'll see whay later)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment