Github / Git Cheat Sheet

Github / Git Cheat Sheet

share:

Here is cheat sheet collection of commands for all your Git needs.

If like me you have a million things going through your head, cheat sheets can be an amazing bible to have to reference.

Take a look at this collection of commands.

Git config

Minimum configuration needed to get up and running

  • git config –global user.name NAME – set user name globally
  • git config –global user.email EMAIL – set user email globally
  • git config user.name || git config user.email – check saved info

Initiate a repository

  • git init – creates a git repository in the directory currently in

Commit a change

  • git commit -m “chg-00134 fixed bug in code” – commits the staging area giving them a specific id
  • git log – shows you all of the commits with details

Git stash you commit

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

  • git stash – clears the changes to the initial state (last commit) & creates a unique id for the current state
  • git stash apply – brings back the current state using git stash multiple times creates a list of stashes of all states with multiple ids
  • git stash list – shows all the stash (States) with their ID
  • git stash apply ID – ID will be the number , which state you want to go back to
  • git stash push -m “Your commit message” – used to give description to stash
  • git stash drop ID – used to remove a stash saved
  • git stash pop ID – applies the specific stash and removes it from history
  • git stash clear – removes all the stash history

Gitignore

A .gitignore file can be created where you can specify all the folders/files that should not be staged and committed. These are always language specific and project related, due to some projects creating custom artifacts locally that you do not want stored in your source control repository.

  • Git Ignore Generator – try this tool to get you started. Github can generate similar files for you when you create a repository using the web console.

Git branches

A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master . As you start making commits, you’re given a master branch that points to the last commit you made. Every time you commit, the master branch pointer moves forward automatically.

  • git branch branch_name – to create a new branch
  • git branch -a – to list all the branches
  • git checkout branch_name – to shift to the other branch
  • git branch -d branch_name – to delete the branch only when it has been merged
  • git branch -D branch_name – to delete the branch (even if not merged to master)
  • git checkout -b branch_name – to create and shift to a new branch at once

Merging

Merging is Git’s way of putting a forked history back together again. The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch. Note that all of the commands presented below merge into the current branch.

  • git merge branchname – this will merge the branch to master (all commits show in master)
  • git merge –squash branchname – this will merge the branch to master (only the commit after merge is shown in master)

Github create a repository

  • Create a new repo on Github and copy the URL
  • Now push your code to it with
  • git push git_url master – pushing code of master branch (to push all branches replace master with –all)
  • creating an alias to not always type URL
  • git remote add origin git_url – origin can be name of anything else, but origin is the word most commonly used
  • git push origin master – to push code to using alias
  • git push -u origin master – pushes and starts tracking the branch (u don’t need to specify it again , ex. if pulling)
  • git clone git_url – will copy the repo to current directory and also add the origin alias by default
  • git remote -v – to check all the aliases made

A tip for managing your Github and git repositories is to utilise great tools such as Visual Studio Code (which is free), which has fantastic source code management integration if you struggle to remember all the commands mentioned above.

For some more great examples check out https://dev.to/zinox9/git-github-cheatsheet-22ok

Here is a tutorial beginners to get you up and running with git

Drop us a message or give us a call

Date: July 6, 2020

Author: Magnetar IT

Inspired to improve your IT? Get in Touch!

Contact Us

Check out our social media: