Github Cheatsheet

Check git version installed on your machine

  git --version

Create a new repository

  1. Go to the project directory
  2. Run git init

       or

Clone an existing repository

Cloning is the process of downloading a copy of the repository from a server.   git clone [repository url]

Note: HTTPS is the recommended url. You might be asked for your Github username and password.
No need to git init in this case as it automatically does it.

Check status of the repository

  git status

WORKFLOW THEORY:
local repository consists of three trees maintained by git.
1. Working Directory which holds the actual files.
2. Index which acts as a staging area. It's like telling git to track.
3. Head which points to the last commit you've made. Technically, points to the branch(master by defalut) which points to the last commit you have made.
So, first any changes in your working directory has to be added to the staging area. After that, you need to finally commit the staging area to the Head.

Add file(s) to the staging area (INDEX)

  git add <filename>[adds modified file]
      or
  git add *[adds all modified files]
      or
  git add '*.py'[adds modified files with .py extension]

Tell git not to track file anymore

If you have manually deleted any file from your working directory and you no longer want to track it
  git rm filename
If the filename is within a folder
  git rm folder/filename
If it was a directory that you deleted
  git rm -r folder

If you have not manually deleted any file but just want to ignore or untrack that file
  git rm --cached filename

To both delete (automatically) as well as untrack file at the same time
  git rm -f filename
Warning: This will delete the file from your working directory

Commit file(s) (HEAD)

  git commit -m "Some message about the changes you made"

Note: The file is committed to the HEAD, but not in your REMOTE repository yet

Push changes to remote (online) repository

  git push -u origin master

  • This is when you have cloned an existing repository.
  • master is the name of the branch and origin is the default name of the repository
  • -u links the local branch with remote branch so that later we can use git pull without arguments

  git push add origin <server_url>

  • This is when you have not cloned an existing repository and want to connect to a new repository.
  • You need to go to github.com, create a new repository and use the repository’s url.

View all branches

  git branch

Create branch

  git branch <feature_x>

Switch to a branch

  git checkout <feature_x>

Create branch and switch to it

  git checkout -b <feature_x>

Delete a branch (soft delete)

  git branch -d <feature_x>

It’ll complain if the branch is not merged

Delete a branch (hard delete)

  git branch -D <feature_x>

It’ll not complain if the branch is not merged

Merge one branch into another

Switch to the branch you want to pull changes into
  git checkout master
Pull changes from another branch
  git merge feature_x

Pull down latest commit from remote to local repository

  git pull origin master

View all remote branches

  git branch --remote

View log (repository history)

  git log

To see commits of a certain author:  git log --author=upen
To see each commit is one line:   git log --pretty=online
To see which files changed in every commit: git log --name-status
To see art tree of all branches:  git log --graph --oneline --decorate --all

Edit the last commit

Let’s say you forgot to add a slight change or missed adding one file to your last commit.
  git add forgotten_file
  git commit --amend
It’ll open the editor to change the commit message. Do as per your need and save changes.
Now, push the amended commit.
  git push -f origin master

Note: Pushing with -f is dangerous. This might mess up your teammates work in the last commit.

To edit a commit message
  git commit --amend -m "New message"

View unstaged changes to files

  git diff

Unstage a file

  git reset filename

Undo last commit and move commit changes to staging

  git reset --soft HEAD^

Undo last commit and drop all your local changes

  git reset --hard HEAD^

Save current changes, without having to stage or commit

  git stash

This is done in case you are wor]ing on something messy and let’s say your boss pops out of nowhere and now you have to show him/her your working feature. You can thus use this command to save all your local and stage changes somewhere, and now you are on last clean commit.

To return to those changes

  git stash pop

For in-depth understanding of git:

Git Pro ebook