Skip to content

Instantly share code, notes, and snippets.

@hickerm
Last active March 13, 2017 18:24

Revisions

  1. hickerm revised this gist Mar 13, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -83,6 +83,10 @@ git reset --hard origin/master (or origin/mo, origin/stg, etc)
    //git fetch downloads the latest from remote without trying to merge or rebase anything
    //git reset resets the master/mo/stg/etc branch to what you just fetched

    git fetch origin master
    git reset —hard FETCH_head
    git clean -df

    //if git reset --hard HEAD leaves untracked files behind
    git clean -f -d
    //to get rid of untracked files and directories in your working copy
  2. hickerm revised this gist Nov 29, 2016. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion git snippets
    Original file line number Diff line number Diff line change
    @@ -101,4 +101,12 @@ git cherry-pick c90fd66
    //not ideal to do this, should be working/merging branches for more sanity

    //search git commit history by keyword (args after --grep are just formatting)
    git log --all --grep='pdf' --pretty=oneline --max-count=25
    git log --all --grep='pdf' --pretty=oneline --max-count=25

    //search individual file to see who changed what line(s)
    //include branch name so it doesn't blame the working copy version so there's no potential for lines not yet being committed
    //http://stackoverflow.com/questions/4638500/git-blame-showing-no-history
    git blame master path_to_file

    //search individual file to see who changed a particular line (or range of lines), only line 24 example below
    git blame -L 24,24 master path_to_file
  3. hickerm revised this gist Jun 3, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion git snippets
    Original file line number Diff line number Diff line change
    @@ -98,4 +98,7 @@ git diff --cache
    //* switch to the bracnh you want to insert the commit into
    //* cherry pick your commit:
    git cherry-pick c90fd66
    //not ideal to do this, should be working/merging branches for more sanity
    //not ideal to do this, should be working/merging branches for more sanity

    //search git commit history by keyword (args after --grep are just formatting)
    git log --all --grep='pdf' --pretty=oneline --max-count=25
  4. hickerm revised this gist May 3, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,9 @@ to paste text into the git bash use the keyboard shortcut, Insert
    //add all files (new or modified) and commit in one command
    git commit --amend -m 'new commit message'

    //add deleted files which are not staged - this will add all modified files
    git add -A

    //show last 5 commits on one line
    git log --pretty=oneline --max-count=5

  5. hickerm revised this gist Apr 1, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,10 @@ git show --pretty="format:" --name-only <part of the commit hash>
    //create a branch and switch to it at same time
    git checkout -b branchname

    //checkout a remote branch that doesn't exist locally
    git fetch --all
    git checkout <branch name>

    //resolve conflicts via visual merge tool
    git mergetool

  6. hickerm revised this gist Oct 14, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,9 @@ git merge --no-commit <branch_to_merge_in>
    git reset path/to/exclude
    git commit

    //undo a merge
    git reset --hard commit_sha

    //see which branches are already merged into the branch you’re on
    //branches on this list without the * in front of them are generally fine to delete with git branch -d
    //as you've already incorporated their work into another branch, so you’re not going to lose anything
  7. hickerm revised this gist Aug 18, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -73,6 +73,10 @@ git reset --hard origin/master (or origin/mo, origin/stg, etc)
    //git fetch downloads the latest from remote without trying to merge or rebase anything
    //git reset resets the master/mo/stg/etc branch to what you just fetched

    //if git reset --hard HEAD leaves untracked files behind
    git clean -f -d
    //to get rid of untracked files and directories in your working copy

    //see what you've changed but not yet staged
    git diff

  8. hickerm revised this gist Feb 12, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git snippets
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    to paste text into the git bash use the keyboard shortcut, Insert

    //add all files (new or modified) and commit in one command
    git commit -am 'commit message'
    git commit --amend -m 'new commit message'

    //show last 5 commits on one line
    git log --pretty=oneline --max-count=5
  9. hickerm revised this gist Feb 12, 2014. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -52,6 +52,12 @@ git push origin new_local_branch_name
    //this assumes your origin remote is configured to hit the github repo
    //which it automatically configures when you use git clone <url>

    //set a local branch up for git pull
    git branch --set-upstream-to origin/st2
    //I had to do this after I created a local st2 branch and pushed it to github
    //see what's configured for git push/pull on with your remotes with:
    git remote show origin

    //force a push to the repo
    git push origin stg --force
    //this assumes you want to force push stg
  10. hickerm revised this gist Feb 5, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion git snippets
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,6 @@ git checkout --track origin/the_remote_branch_name

    //push a local branch to github
    git push origin new_local_branch_name
    //haven't tested this (yet), but I think this is correct
    //this assumes your origin remote is configured to hit the github repo
    //which it automatically configures when you use git clone <url>

  11. hickerm revised this gist Jan 22, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -53,6 +53,10 @@ git push origin new_local_branch_name
    //this assumes your origin remote is configured to hit the github repo
    //which it automatically configures when you use git clone <url>

    //force a push to the repo
    git push origin stg --force
    //this assumes you want to force push stg

    //rename a local branch
    git branch -m <oldname> <newname>
    //if you want to rename the current branch
  12. hickerm revised this gist Jan 10, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,11 @@ git branch -v
    git checkout stg
    git merge bug_29868

    //exclude a directory from merge
    git merge --no-commit <branch_to_merge_in>
    git reset path/to/exclude
    git commit

    //see which branches are already merged into the branch you’re on
    //branches on this list without the * in front of them are generally fine to delete with git branch -d
    //as you've already incorporated their work into another branch, so you’re not going to lose anything
  13. hickerm revised this gist Jan 10, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,9 @@ git commit -am 'commit message'
    //show last 5 commits on one line
    git log --pretty=oneline --max-count=5

    //list all the files for a commit
    git show --pretty="format:" --name-only <part of the commit hash>

    //create a branch and switch to it at same time
    git checkout -b branchname

  14. hickerm revised this gist Jan 9, 2014. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -39,6 +39,12 @@ git branch -d the_local_branch
    //fetch a remote branch, creates a local branch that tracks a remote branch
    git checkout --track origin/the_remote_branch_name

    //push a local branch to github
    git push origin new_local_branch_name
    //haven't tested this (yet), but I think this is correct
    //this assumes your origin remote is configured to hit the github repo
    //which it automatically configures when you use git clone <url>

    //rename a local branch
    git branch -m <oldname> <newname>
    //if you want to rename the current branch
  15. hickerm revised this gist Jan 9, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -30,6 +30,9 @@ git branch --merged
    //if you try to delete these, it will fail as a safety net, but you can force the deletion with -D
    git branch --no-merged

    //undo a git merge with conflicts
    git merge --abort

    //delete a local branch
    git branch -d the_local_branch

  16. hickerm revised this gist Jan 2, 2014. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -41,6 +41,12 @@ git branch -m <oldname> <newname>
    //if you want to rename the current branch
    git branch -m <newname>

    //force pull - revert your local to whatever the repo is
    git fetch --all
    git reset --hard origin/master (or origin/mo, origin/stg, etc)
    //git fetch downloads the latest from remote without trying to merge or rebase anything
    //git reset resets the master/mo/stg/etc branch to what you just fetched

    //see what you've changed but not yet staged
    git diff

  17. hickerm revised this gist Dec 26, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -38,6 +38,8 @@ git checkout --track origin/the_remote_branch_name

    //rename a local branch
    git branch -m <oldname> <newname>
    //if you want to rename the current branch
    git branch -m <newname>

    //see what you've changed but not yet staged
    git diff
  18. hickerm revised this gist Dec 18, 2013. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    to paste text into the git bash use the keyboard shortcut, Insert

    //add all files (new or modified) and commit in one command
    git commit -am 'commit message'

    //show last 5 commits on one line
    git log --pretty=oneline --max-count=5

  19. hickerm revised this gist Dec 11, 2013. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -33,6 +33,9 @@ git branch -d the_local_branch
    //fetch a remote branch, creates a local branch that tracks a remote branch
    git checkout --track origin/the_remote_branch_name

    //rename a local branch
    git branch -m <oldname> <newname>

    //see what you've changed but not yet staged
    git diff

  20. hickerm revised this gist Dec 6, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    to paste text into the git bash use the keyboard shortcut, Insert

    //show last 5 commits on one line
    git log --pretty=oneline --max-count=5

  21. hickerm revised this gist Nov 20, 2013. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,12 @@ git branch -d the_local_branch
    //fetch a remote branch, creates a local branch that tracks a remote branch
    git checkout --track origin/the_remote_branch_name

    //see what you've changed but not yet staged
    git diff

    //see what you've staged that will go into your next commit
    git diff --cache

    //cherry pick changes
    //* copy the SHA hash from the commit/branch you want (either something like the first 6 or 7 characters from the bash or the entire thing via github)
    //* switch to the bracnh you want to insert the commit into
  22. hickerm revised this gist Nov 18, 2013. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,9 @@ git branch --no-merged
    //delete a local branch
    git branch -d the_local_branch

    //fetch a remote branch, creates a local branch that tracks a remote branch
    git checkout --track origin/the_remote_branch_name

    //cherry pick changes
    //* copy the SHA hash from the commit/branch you want (either something like the first 6 or 7 characters from the bash or the entire thing via github)
    //* switch to the bracnh you want to insert the commit into
  23. hickerm revised this gist Nov 1, 2013. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion git snippets
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,12 @@ git mergetool
    //show last commit on each branch
    git branch -v

    //merge branch
    //check out the branch you wish to merge into and then run the git merge command
    //this scenario would be working on the bug_29868 branch and it's ready to merge/deploy/test
    git checkout stg
    git merge bug_29868

    //see which branches are already merged into the branch you’re on
    //branches on this list without the * in front of them are generally fine to delete with git branch -d
    //as you've already incorporated their work into another branch, so you’re not going to lose anything
    @@ -27,4 +33,4 @@ git branch -d the_local_branch
    //* switch to the bracnh you want to insert the commit into
    //* cherry pick your commit:
    git cherry-pick c90fd66
    //not ideal to do this, should be working/merging branches for more sanity.
    //not ideal to do this, should be working/merging branches for more sanity
  24. hickerm revised this gist Nov 1, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion git snippets
    Original file line number Diff line number Diff line change
    @@ -26,4 +26,5 @@ git branch -d the_local_branch
    //* copy the SHA hash from the commit/branch you want (either something like the first 6 or 7 characters from the bash or the entire thing via github)
    //* switch to the bracnh you want to insert the commit into
    //* cherry pick your commit:
    git cherry-pick c90fd66
    git cherry-pick c90fd66
    //not ideal to do this, should be working/merging branches for more sanity.
  25. hickerm revised this gist Nov 1, 2013. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion git snippets
    Original file line number Diff line number Diff line change
    @@ -20,4 +20,10 @@ git branch --merged
    git branch --no-merged

    //delete a local branch
    git branch -d the_local_branch
    git branch -d the_local_branch

    //cherry pick changes
    //* copy the SHA hash from the commit/branch you want (either something like the first 6 or 7 characters from the bash or the entire thing via github)
    //* switch to the bracnh you want to insert the commit into
    //* cherry pick your commit:
    git cherry-pick c90fd66
  26. hickerm revised this gist Nov 1, 2013. 1 changed file with 19 additions and 1 deletion.
    20 changes: 19 additions & 1 deletion git snippets
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,22 @@
    git log --pretty=oneline --max-count=5

    //create a branch and switch to it at same time
    git checkout -b branchname
    git checkout -b branchname

    //resolve conflicts via visual merge tool
    git mergetool

    //show last commit on each branch
    git branch -v

    //see which branches are already merged into the branch you’re on
    //branches on this list without the * in front of them are generally fine to delete with git branch -d
    //as you've already incorporated their work into another branch, so you’re not going to lose anything
    git branch --merged

    //see which branches you haven’t yet merged in
    //if you try to delete these, it will fail as a safety net, but you can force the deletion with -D
    git branch --no-merged

    //delete a local branch
    git branch -d the_local_branch
  27. hickerm revised this gist Oct 31, 2013. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion git snippets
    Original file line number Diff line number Diff line change
    @@ -1 +1,5 @@
    git log --pretty=oneline --max-count=5
    //show last 5 commits on one line
    git log --pretty=oneline --max-count=5

    //create a branch and switch to it at same time
    git checkout -b branchname
  28. hickerm created this gist Oct 23, 2013.
    1 change: 1 addition & 0 deletions git snippets
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    git log --pretty=oneline --max-count=5