Git Commands

Git Commands

Basic Workflow

Initializing a blank repository

git init

Making, committing, and pushing changes

Make sure you have the most up-to-date code (important!)

git pull

Make a new branch (optional)

git checkout -b my_branch

Confirm that you’re on the new branch

git status

Make changes to files

See which files are modified

git status

See the changes to the files

git diff
git diff -w # ignore whitespace changes
git diff filename.php filename.css # only changes to specific files

Go through and remove debugging code, fix typos, etc.

Now add the files to be committed

git add filename.php filename.css

Check to see the files are going to be committed

git status

Now commit the changes

git commit -m "Fixed display issues in filename.php"

Check and see that your branch is now ahead of origin

git status

Now push the new commit

git push origin branch_name

Pulling changes to another environment e.g. live

Check what status and what revision you’re on in case you need to revert; also check for locally modified files

git status
git rev-parse—short HEAD

Pull the changes from the remote repository and note whether files are changed in the current branch

git pull

Verify that the new revision is now in place

git status
git rev-parse—short HEAD

Deleting Branches

Delete local branch:

git branch -D my-branch

Delete remote branch:

git push origin—delete my-branch

Undoing Things

Unstange modified file for commit so it moves back into modified status

git reset—<filePath>

Git can’t tell if you’re talking about the branch or the file (each of which has very different consequences), so the makes it clear we’re not talking about a branch and that we want to reset our file.

http://andrewberls.com/blog/post/git-tricks-unstaging-files

Rebasing commits

First look at log of commits:

git log—oneline

Rebase by number of commits:

git rebase -i HEAD~13

Or rebase to last good commit (7e3d497):

git rebase -i 7e3d497

Or rebase to commit before first bad commit:

git rebase—interactive 796ebf8^

Config

Origin, username

SSH Origin

git remote add origin git@domain.com:path/to/repo.git

You can make the above change after the fact by editing the origin manually:

vim .git/config #:
[remote "origin"]
    url = git@domain.com:/path/to/repo.git

Add the host to your .ssh/config so it uses your key every time and you don’t need to provide credentials:

Host domain.com
    Hostname git@domain.com
    IdentityFile ~/.ssh/domain.id_rsa

HTTP Origin

git remote add origin https://(user@)domain.com/path/to/repository

Save Username and Password (DON’T DO THIS) (Warning: this stores credentials in ~/.git-credentials in plaintext!)

git config—global credential.helper store
git pull # then enter credentials and it will save them

Set Username in config (does nothing?)

git config—global user.name "twhitney"

Color

Set git to have color by default:

git config—global color.ui true

Or use this if that doesn’t work:

git config—global color.diff auto
git config—global color.diff always

Misc

Diffs

Super useful: git diff with color on files outside of git:

git diff—no-index file1 file2
git diff—color-words—no-index file1 file2 # if color isn't enabled by default
diff -U 5 file1 file2

After checkout, to show files changed by checkout

git diff—name-status HEAD@{1} HEAD

Show difference before checking out and see what the checkout will modify.

git diff—name-status <branch_switching_to>

Show changes in last commit:

git show

Logging

Show outgoing, like with hg outgoing:

git log—branches—not—remotes=origin

Show pretty history tree:

git log—all—graph—decorate—oneline
git log—branches—not—remotes
git log—branches—not—remotes—simplify-by-decoration—decorate—oneline
git fetch
git log origin/master..HEAD
git reset HEAD~
git reset—hard HEAD~
git reset—hard origin/master

Find variable changed yesterday:

git log -p—since yesterday | grep varName

Find controllers changed yesterday:

git log—oneline—showfile | grep controllers

What files did I work on last week:

git log—name-only—oneline—author tucker—since 1.weeks

How many JS commits did I do last month?

git log—since 1.months—author tucker—name-only | grep -i '\.js$' | wc -l

How many JS commits did I do on each file last month?

git log—since 1.months—author tucker—name-only | grep -i '\.js$' | awk '{arr[$1]++} END {for(i in arr) print arr[i]," - ",i}' | sort -r -n