Stashing is the term git uses for saving your changes before taking pull.
# save / stash your changesgitstashsave"<stash-name>"# list of stashed changesgitstashlist# retrieve / put back stashed changesgitstashapplystash@{index}# where index is the position of stash shown in stash list# delete stashed/saved changesgitstashdrop"<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 branchgitcheckout-b<branch-name>gitpushorigin<branch-name>--set-upstream# show branchesgitbranch# show local & remote branchesgitbranch-a# get a remote branchgitfetchorigingitcheckout--trackorigin/<branch-name># merge branchgitmerge<branch-name># list merged branchesgitbranch-a--merged# delete local remote-tracking branchesgitremotepruneorigin# delete local branchgitbranch-D<branch-name># delete remote branchgitbranch-r-d<branch-name># delete local & remote branchgitpushorigin--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 branchgitcheckout-b<feature-branch># checkout/navigate to your branchgitcheckout<feature-branch># make changes, commit and pushgitadd-Agitcommit-m"new feature updates"gitpushorigin<feature-branch>
Rebasing â Rebase to make sure we sync our feature branch with master
# navigate/checkout to mastergitcheckoutmaster# pull to get latest code updategitpulloriginmaster# navigate back & rebasegitcheckout<feature-branch>gitrebasemaster# incase of conflict, make changes and just add (don't commit)gitadd-Agitrebase--continue# once the rebase is completed, do force pushgitpushorigin<feature-branch>--force-with-lease
Squash â all you branch commits to one commit with meaningful message
# Method 1gitresetorigin/<branch-you-branched-from># usually `git reset origin/master`gitadd--allgitcommit-m"new feature / bug detail"gitpushorigin<branch-name>--force-with-lease# Method 2gitcheckoutmastergitmerge--squash<feature-branch># at this point everything is merged, not committedgitadd-Agitcommit-m"Feature"gitpushoriginmaster
Merge your branch to master
# checkout/navigate to master branchgitcheckoutmaster# merge your code to master. PS. using this will auto-close PRgitmerge<feature-branch>gitpushoriginmaster# delete your branchgitbranch-D<feature-branch>
Commit
# delete last remote commitgitreset--hardHEAD~1gitpushoriginHEAD--force-with-lease# delete local commitsgitreset--hardHEAD~1# rollback latest remote commitgitreset--hardHEAD^# rollback to a specific commitgitreset--hard<commit-sha># amend last commit messagegitcommit--amend--author="<github-username>"# amend last commit messagegitcommit--amend-m"New commit message"
Tag
# create a new taggittag-av1.0.0-m"my version 1.0.0"# show tag detailsgitshow<tagname># push tag to remotegitpushorigin<tagname># list tagsgittaggittag-l"v1.8*"# show all tags starting with 1.8# delete taggittag-d<tagname>#localgitpushorigin--delete<tagname>#remote
Amend last remote commit
gitreset--softHEAD^# Reset branch head to previous commit.gitstash# Stash the last commit.gitpush-forigin<branch-name># Force push to remote. The remote now doesn't have the last commit.gitstashpop# Pop your stash.gitcommit-a# Commit cleanly.gitpushorigin<branch-name># Push to remote.
Multiple github accounts
# add new account for companyssh-keygen-trsa-C"company_email_id@domain.com"~/.ssh/id_rsa_company# create configtouch~/.ssh/configvimconfig# Config file content should have# default accountHostgithub.comHostNamegithub.comUsergitIdentityFile~/.ssh/id_rsa# company accountHostgithub-COMPANYHostNamegithub.comUsergitIdentityFile~/.ssh/id_rsa_company# Do it on github website - add ssh keys to github account# add origin for projectsgitremoteaddorigingit@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:
Move git files with commits from the old to the new repo
Navigate to the old repo path eg. cd /works/old-repo/
# remove old remotegitremotermorigin# make sure remote is emptygitremote-v# add new repo remotegitremoteaddorigingit@github.com:<github-username>/<new-repo-name>.git# push your changes to new repo with commitsgitpush-uorigin--all# add origin for projectsgitremoteaddorigingit@github-COMPANY:<github-username>/<repository-name>.git