Git

From Adam Colton
Jump to navigation Jump to search

http://byte.kde.org/~zrusin/git/git-cheat-sheet.svg

Cheat sheet

undo commit

 $ git commit -m "Something terribly misguided"
 $ git reset HEAD~
 << edit files as necessary >>
 $ git add ...
 $ git commit -c ORIG_HEAD

find in history

 git log -S'the line from your file' -- path/to/your/file.txt

unstage

 git reset <file>

Commands

Standards Used: these are git commands, there for they come after "git". Anything in parenthesis are arguments, the parenthesis are not included. Anything after : is an explanation.

  • init : initializes a git repository.
  • status : shows unstaged changes
  • add (filename) : stages a change
    • add * : stages all changes
    • add . : ??? I think this stages all files (good for new repository)
    • -p : asks what chunks of the file you want to add, good for small checkins
  • reset
    • (filename) : Unstages a change
    • --hard (commit id/tag) : rollback to that commit
  • commit : commit staged changes
    • -m ("comment")
    • -a : commit all (skips staging)
  • log : shows the history of the repository
    • --skip=n : skips the last n commits
    • --since=yyyy-mm-dd
    • --until=yyyy-mm-dd
    • --grep="(pattern)"
    • --graph : shows a branch graph
  • checkout
    • checkout (id/tag) : goes back to a specific version
    • checkout master : returns to the latest changes
    • checkout -b (branch) : checks out a branch creates the branch if it does not exist
  • reset (id/tag) : erases all history after (id), basically a rollback
  • help : list all commands
    • help (operation/keyword) get help on a specific command
  • clone (repo directory) : clones the repo to your local machine, if a directory is specified, it will be copied there, otherwise it will create a directory with the repo name
  • fetch
  • merge : combine two branches
  • push : pushes to remote - I'm not clear which, how, what if there are multiple - much confusion.
  • remote
  • add origin (url)
  • pull : really just a shortcut for fetch then merge
  • clean : i dont know
  • tag : lists all the tags in the repo
    • tag (tag) : tags the last commit
  • branch : lists the branches
    • branch (branchName) : creates a branch, but stays in the current branch, use checkout -b instead
    • branch -d (branchName) : deletes a branch
  • diff : shows a diff of all unstaged changes

Examples

 cd /path/to/your/Workbench/repo
 git remote add origin https://your_repo_identity_here/online_workbench.git
 git push -u origin master

for ssh

 ssh-agent bash
 ssh-add keyfile.pem
 git init
 git remote add origin user@host.dom:/path/to/repo/
 git pull origin branch

To clone a repo to a server for ssh access

 -- on the server --
 git --bare init
 -- on the client --
 ssh-agent bash
 ssh-add keyfile.pem
 git remote add origin user@host.dom:/path/to/repo/
 git push origin branch

Concepts

Repository

The Git representation of the history of a set of files in a directory tree.

Stage

Changes are made locally, staged and then committed. Staging is the process of preparing part (or all) of your change to be committed.

Checkout

This is not like the MS version - you are not getting a file to edit it, you are pulling a previous version of a file.

Remote

A git server. This is where I get confused.

SSH

I've been using ssh key files (pem) because that's what Amazon uses and it seems like a better practice than passwords. The best method I've found for this is to run bash in ssh-agent:

 ssh-agent bash

You're now running bash-in-bash, a little odd but it works. Now add a key (ssh-add keyfile) and all the remote commands will work.

Origin

...and origin master...more confused

Branching

A branch name cannot contain whitespace or :?!^*[ or two consecutive dots. Forward slash is used to denote heirarchical naming.

Misc

HEAD : pointer to the active branch. Master : The default name of a new repository - because it will be the "main" branch. Everything is a branch. Origin : The name of the remote when a repository is cloned

Bare

This is a repo containing just the repo data and no working files. This is how you would configure a "Server", particularly good for syncing up users. To be clear, you can clone a non-bare repository from a bare one.

Setup

 apt-get install git-core
 git config --global user.name "John Doe"
 git config --global user.email johndoe@example.com

Rebase

 git rebase dev

Do this from master after dev work is done

Tests

  • Make repository
  • Branch
  • Merge
  • Patch
  • History re-write
  • rebase
  • clone
  • push
  • pull