Quantcast
Channel: Troy Grosfield
Viewing all articles
Browse latest Browse all 28

Git Cheat Sheet

$
0
0

Git commands and working with Github.

Create a Repository

Steps to create a new repository in github.

  1. Go to github and log in. In the top right you’ll see an icon to create a repository. Click that link.
  2. Enter a repository name. Separate words with dashes (i.e. my-first-repo)
  3. Enter a description for what the repository is for.
  4. Choose to make the repo public or private.
  5. Click the “Create Repository” button.

Clone a Repository Locally

This clones a repo to your local computer.

  1. Go to the github repo you just created.  You’ll see a url to your repo.
  2. Since we have already generated the ssh key, you can select the “ssh” button which will give you the ssh git url for the repository.  Copy that url.
  3. Run the following command:
    $ git clone git@github.com:path-to-repository/repository_name.git
    Cloning into 'repository_name'...
    Warning: Permanently added the RSA host key for IP address '10.123.123.123' to the list of known hosts.
    remote: Counting objects: 3, done.
    remote: Compressing objects: 100% (2/2), done.
    remote: Total 3 (delta 0), reused 0 (delta 0)
    Receiving objects: 100% (3/3), done.

Branching

Create a Branch

$ git branch my_branch_name

or

$ git checkout -b my_branch_name

Create a Branch From Another Branch

$ git checkout -b <my_new_branch_name> <from_branch_name>

Push a Branch to github

$ git push origin my_branch_name

Delete a Branch

$ git branch -d my_branch_name

Add Files to git

Adding files to git merely tells git you want to put those files under version control.  It doesn’t actually add the files or folders to the repo.

Add a file:

$ git add my_file.py

Add a folder:

$ git add my_folder_name

Committing

Committing to git (local cloned repo)

Commit files once they’ve been added. (Note this only commits to your local cloned repo).

$ git commit my_file.py

or commit a folder:

$ git commit my_folder_name

Committing to git (remote repo)

This pushes all the changes from your local repo to the actual (remote) repo.

$ git push origin my_branch_name

Merging Branch into Master (via github)

  1. Go to your repository on github
  2. Click on the “Pull Request” button
  3. Select the branch you want to merge to master
  4. Review the changes
  5. Accept pull request to merge the changes in branch into master

Merge One Branch into Another (via command line)

For this step, I’ll demonstrate merging the contents of “branch_A” into “working_branch”.  First, make sure both your branches are up to date and you’re working branch is checked in:

$ git checkout branch_A

Update your working branch with the latest:

$ git pull origin

Merge “branch_A” into “working_branch”:

$ git merge --no-commit branch_A

Then if all went well, hopefully you see something like the following:

Automatic merge went well; stopped before committing as requested

Update with Remote Repo: Pull

Let’s say changes have happened on the remote server and you want to sync your local repository with those changes:

$ git pull origin

Update with Branch with Another Branch

Let’s say I created a branch off of a repo called “develop” and wanted to update my branch with the latest changes in “develop”:

$ git pull origin <branchName>
$ git pull origin develop

Delete a Local Branch

Delete a branch you’re working on locally (doesn’t affect the remote branch):

$ git branch -D <branchName>

Delete a Remote Branch

Deleting a remote branch by using:

$ git push origin --delete <branchName>

Creating a Tag

Update your code to the latest:

$ git pull origin

Create the tag locally:

$ git tag -a YOUR_TAG_NAME -m "Tag comment for the release"

Push your tag to github:

$ git push origin YOUR_TAG_NAME
Counting objects: 1, done.
Writing objects: 100% (1/1), 185 bytes, done.
Total 1 (delta 0), reused 0 (delta 0)
To git@github.com:YOUR-ORG-NAME/repo-name.git
 * [new tag]         YOUR_TAG_NAME -> YOUR_TAG_NAME

Deleting a Tag

Update your code to make sure you have the tag info locally:

$ git pull origin
From github.com:YOUR_USERNAME/YOUR_REPO_NAME
 * [new tag]     YOUR_TAG_NAME   -> YOUR_TAG_NAME
Already up-to-date.

Delete the tag locally:

$ git tag -d YOUR_TAG_NAME
Deleted tag 'YOUR_TAG_NAME' (was bbdb03f)

Delete the tag on github:

$ git push origin :YOUR_TAG_NAME
To git@github.com:YOUR_USERNAME/YOUR_REPO_NAME.git
 - [deleted]         YOUR_TAG_NAME

Creating a Diff

Create a diff file and redirect output to another file:

$ git diff master..YOUR_BRANCH_NAME >> the_branch_diff.txt

Creating a Submodule

$ git submodule add git://github.com/some-repo/repo-name.git repo-name
Initialized empty Git repository in /path/to/repo/repo-name/.git/
remote: Counting objects: 3181, done.
remote: Compressing objects: 100% (1534/1534), done.
remote: Total 3181 (delta 1951), reused 2623 (delta 1603)
Receiving objects: 100% (3181/3181), 675.42 KiB | 422 KiB/s, done.
Resolving deltas: 100% (1951/1951), done.

Updating a Submodule to Specific Revision

$ cd path/to/submodule/root/
$ git pull origin
$ git checkout v3.0.2
Previous HEAD position was 13b9667... Merge pull request #11335 from xyz-repo/version-number-updater
HEAD is now at 3887f54... update remaining version numbers
$ path/to/submodule/root/: cd ..
$ git add twitter_bootstrap

Syncing a Fork

Follow the instructions on github:

  • https://help.github.com/articles/configuring-a-remote-for-a-fork
  • https://help.github.com/articles/syncing-a-fork

Help

$ git help

Working with pip

Installing from a specific repository branch. In this example, let’s say I want to pip install django at the stable 1.6.x branch:

pip install git+git://github.com/django/django.git@stable/1.6.x

Viewing all articles
Browse latest Browse all 28

Latest Images

Trending Articles





Latest Images