You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The default branch name in Git is master. Use feature branches as part of your workflow.
View Branches
$ git branch # Locals
$ git branch -r # Remotes
$ git branch -a # All
Create a Branch
$ git branch <new-branch># Create new-branch but stay on current branch
$ git checkout -b <new-branch># Create new-branch and checkout 'new-branch'
Rename Branches
$ git branch -m <new-name># Rename branch you are currently in
$ git branch -m <current-name><new-name># Rename from any branch
Delete Branches
$ git branch -d <branch># Local (uppercase '-D' removes uncommited changes too)
$ git push <remote> :<branch># Remote (the colon can be substituted for '--delete ')
Checkout a new remote branch
$ git fetch <remote><branch># Pull down latest changes into the index
$ git checkout <new-remote-branch-name># Checkout local version of new remote branch
Nice 'Stash
Stashing allows you to save uncommited changes (staged and unstaged)
View Stashes
$ git stash list
$ git stash list --stat # View details of stashes listed
$ git stash show # View details of most recent stash
Add a message to your stash
$ git stash save 'message goes here'
Create a new branch with your most recent stash
$ git stash branch <new-branch> stash@{0}
Only stash unstaged changes and preserve the staging area
Note: this will not stash untracked files
$ git stash --keep-index
Stash everything, including untracked files
$ git stash --include-untracked === git stash -u
Shave that stash list
$ git stash clear # Deletes all saved stashes
Shortcuts
$ git stash === git stash save
$ git stash apply === git stash apply stash@{0}
$ git stash drop === git stash drop stash@{0}
$ git stash pop === git stash apply AND git stash drop
Re'set Yourself
Make changes to or remove previous commits
These commands move HEAD back 1 commit as indicated by HEAD^
$ git reset --soft HEAD^ # Undo last commit and keep modified files staged
$ git reset --hard HEAD^ # Undo last commit and delete changes
$ git reset --mixed HEAD^ # Undo last commit and unstage everything (default if type not specificed)
Move back further using the tilde HEAD~2, HEAD~6, etc
Reset to a specific point in history
$ git reset --hard <SHA>
Alternatives to $ git reset
Revert: remove a specific commit but preserve the history of surrounding commits
$ git revert <SHA>
Amend: add changes and updated commit message to last commit
$ git commit --amend -m 'Updated message'
Running this command without any changes is a quick way to update the last commit message
Cherry Picking FTW!
Copy commits from other branches into current branch
$ git cherry-pick <SHA># Opens editor so you can update commit message
$ git cherry-pick --edit <SHA># Combines changes and adds to staging area uncommited (so you can make changes)
$ git cherry-pick --no-commit <SHA-1><SHA-2>
$ git commit -m 'squashing some cherries'# Track which commit you cherry picked from (since new SHA is generated)
$ git cherry-pick -x <SHA># Track original committer and the cherry picker
$ git cherry-pick --signoff <SHA>
Holy Reflog.. Git never forgets.
View your "deleted" history of each time HEAD moved
$ git reflog
$ git log --walk-reflogs # View detailed history of change
Ungit yo-self
After cloning a repo you might want to create a fresh one for yourself
$ git commit -m # Create commit message to staged files
$ git commit -am # Add tracked files with changes and create commit message
$ git add -A # Add all files including untracked
$ git add *name*# Add all files that contain 'name' to staging
$ git add dir/folder # Add all files in directory dir/folder to staging
Remove branches that have already been merged with master
Thanks for helping me out. Your repo is really awesome for those who are looking to learn advanced git commands.