Stashing is the term git uses for saving your changes before taking pull.
# save / stash your changes
git stash save "<stash-name>"
# list of stashed changes
git stash list
# retrieve / put back stashed changes
git stash apply stash@{index} # where index is the position of stash shown in stash list
# delete stashed/saved changes
git stash drop "<stash-name>"
Note: <stash-name> is needed if you work on multiple saves otherwise you can skip and type git stash to save and git stash apply or git stash pop to retrieve.
Branch
# create a new branch
git checkout -b <branch-name>
git push origin <branch-name> --set-upstream
# show branches
git branch
# show local & remote branches
git branch -a
# get a remote branch
git fetch origin
git checkout --track origin/<branch-name>
# merge branch
git merge <branch-name>
# list merged branches
git branch -a --merged
# delete local remote-tracking branches
git remote prune origin
# delete local branch
git branch -D <branch-name>
# delete remote branch
git branch -r -d <branch-name>
# delete local & remote branch
git push origin --delete <branch-name>
Merge & Rebasing
Branches are good for developing features to your software, the below steps walk you through creating, rebasing and merging.
Creation
# from master branch create your feature branch
git checkout -b <feature-branch>
# checkout/navigate to your branch
git checkout <feature-branch>
# make changes, commit and push
git add -A
git commit -m "new feature updates"
git push origin <feature-branch>
Rebasing — Rebase to make sure we sync our feature branch with master
# navigate/checkout to master
git checkout master
# pull to get latest code update
git pull origin master
# navigate back & rebase
git checkout <feature-branch>
git rebase master
# incase of conflict, make changes and just add (don't commit)
git add -A
git rebase --continue
# once the rebase is completed, do force push
git push origin <feature-branch> --force-with-lease
Squash — all you branch commits to one commit with meaningful message
# Method 1
git reset origin/<branch-you-branched-from> # usually `git reset origin/master`
git add --all
git commit -m "new feature / bug detail"
git push origin <branch-name> --force-with-lease
# Method 2
git checkout master
git merge --squash <feature-branch> # at this point everything is merged, not committed
git add -A
git commit -m "Feature"
git push origin master
Merge your branch to master
# checkout/navigate to master branch
git checkout master
# merge your code to master. PS. using this will auto-close PR
git merge <feature-branch>
git push origin master
# delete your branch
git branch -D <feature-branch>
Commit
# delete last remote commit
git reset --hard HEAD~1
git push origin HEAD --force-with-lease
# delete local commits
git reset --hard HEAD~1
# rollback latest remote commit
git reset --hard HEAD^
# rollback to a specific commit
git reset --hard <commit-sha>
# amend last commit message
git commit --amend --author="<github-username>"
# amend last commit message
git commit --amend -m "New commit message"
Tag
# create a new tag
git tag -a v1.0.0 -m "my version 1.0.0"
# show tag details
git show <tagname>
# push tag to remote
git push origin <tagname>
# list tags
git tag
git tag -l "v1.8*" # show all tags starting with 1.8
# delete tag
git tag -d <tagname> #local
git push origin --delete <tagname> #remote
Amend last remote commit
git reset --soft HEAD^ # Reset branch head to previous commit.
git stash # Stash the last commit.
git push -f origin <branch-name> # Force push to remote. The remote now doesn't have the last commit.
git stash pop # Pop your stash.
git commit -a # Commit cleanly.
git push origin <branch-name> # Push to remote.
Multiple github accounts
# add new account for company
ssh-keygen -t rsa -C "company_email_id@domain.com"
~/.ssh/id_rsa_company
# create config
touch ~/.ssh/config
vim config
# Config file content should have
# default account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# company account
Host github-COMPANY
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_company
# Do it on github website - add ssh keys to github account
# add origin for projects
git remote add origin git@github-COMPANY:<github-username>/<repository-name>.git
A common mistake is cloning using the default (HTTPS) instead of SSH. You can correct this by going to your repository, clicking the ssh button left to the URL field and updating the URL of your origin remote like this: