Git CheatSheat

February 16, 2019 2 minutes

Below is a list of some common git commands (incremrental list; will keep adding more commands). The list is prepared from various internet forums and blog posts :

Create a new local repository

Create a new blank repository that will host files that require to be version controlled : git init

Create a local copy of remote repository

Check out a repository for local code changes. This will be an exac copy of the remote repo that you can sync from at regular intervals : git clone /path/to/repository

Add files

Add one or more files to staging (index) :

# add a specific file
git add <file_name>

# add all files, except the hidden or . files
git add * 

# add everything including hidden or .files
git add .
Commit
  • Commit changes to head (but not yet to the remote repository): git commit -m "Commit message"
  • Commit any files you’ve added with git add, and also commit any files you’ve changed since then : git commit -a
Push

Send changes to the master branch of your remote repository : git push origin master

Status

List the files you’ve changed and those you still need to add or commit : git status

Remote Repositories
  • If you haven’t connected your local repository to a remote server, add the server to be able to push to it : git remote add origin <server>
  • List all currently configured remote repositories : git remote -v
Branches
  • Create a new branch and switch to it : git checkout -b <branchname>
  • Switch from one branch to another : git checkout <branchname>
  • List all the branches in your repo, and also tell you what branch you’re currently in : git branch
  • Delete the feature branch : git branch -d <branchname>
  • Push Code Changes : git push origin <branchname>
  • Delete a branch on your remote repository: git push origin :<branchname>
Updates
  • Fetch and merge changes on the remote server to your working directory : git pull
  • To merge a different branch into your active branch : git merge <branch_name>
  • Undo last merge before commiting : git merge --abort
  • View all the merge conflicts : git diff
  • Preview changes, before merging : git diff <source_branch_name> <target_branch_name>
  • Undo local changes : git checkout -- <filename>
  • Drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it : git fetch origin && git reset --hard origin/master

comments powered by Disqus