Skip to content

Instantly share code, notes, and snippets.

@hickerm
Last active March 13, 2017 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hickerm/7126408 to your computer and use it in GitHub Desktop.
Save hickerm/7126408 to your computer and use it in GitHub Desktop.
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
//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
//checkout a remote branch that doesn't exist locally
git fetch --all
git checkout <branch name>
//resolve conflicts via visual merge tool
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
//exclude a directory from merge
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
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
//undo a git merge with conflicts
git merge --abort
//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
//push a local branch to github
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
//rename a local branch
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
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
//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
//* cherry pick your commit:
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
//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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment