Skip to content

Instantly share code, notes, and snippets.

@codeopensrc
Last active February 3, 2017 01:25
Show Gist options
  • Save codeopensrc/ab4c241d4c7b89978069 to your computer and use it in GitHub Desktop.
Save codeopensrc/ab4c241d4c7b89978069 to your computer and use it in GitHub Desktop.
Some git CLI help

Have server working directory based off of bare git repo

In the bare git-repo on the server
  • Change post-update.sample to post-update
  • Have the below command inside post-update
    GIT_WORK_TREE=/home/path/to/working/dir/mytest git checkout -f

List latest commit sha

git rev-parse --short <branchname>

Working on dev, several commits ahead of master, check what you've added

git diff master..dev

Upstream branch several commits ahead, check whats been added

git diff dev..origin/master
OR
git diff master..origin/master

Upstream branch several commits ahead, you have some uncommitted changes (local configs for example)

And you want to update to upstream branch

git stash
git pull
git stash pop And resolve any local conflicts

Reverting commits. Source

Reset to previous commit

git reset <commit> will reset your INDEX and NOT your files in your working tree back to <commit>/HEAD~#
AND the files will NOT be in the staging area (will be red instead of green with git status)

Like git reset except the files will aready be in the staging area ready for commit (green instead of red)

[] brackets denote choosing between one of the noted parameters

git reset --soft [<commit>, HEAD~#] Where # is num of commits

This will reset your INDEX AND files in your working tree, and cannot be undone[1]

NOTE: THIS IS DANGEROUS IF YOU DO NOT KNOW WHAT YOU ARE DOING
[] brackets denote choosing between one of the noted parameters

git reset --hard [<commit>, HEAD~#] Where # is num of commits

Undo git reset (NOT --hard, that is permanent)[1]

git reset HEAD@{1}

To merge and either auto resolve with theirs or ours. Source

To accept their changes:

git merge --strategy-option theirs

To accept yours:

git merge --strategy-option ours

Stop tracking an already tracked file (changes frequently / each member has their own local copy)

git update-index --assume-unchanged [path/to/file]

Add specific hunks/lines

git add -i
Use the ? command to explain each command
When attempting to stage individual lines, use the e command when staging hunks and follow the notes in the editor

[1] The only way I know how to reset a --hard reset is using the git reflog command. Even then it only shows up for 90 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment