Skip to content

Instantly share code, notes, and snippets.

@phanendraguptha
Last active August 18, 2019 07:58
Show Gist options
  • Save phanendraguptha/f552fc4d6744cbd23d8ac46cb2124758 to your computer and use it in GitHub Desktop.
Save phanendraguptha/f552fc4d6744cbd23d8ac46cb2124758 to your computer and use it in GitHub Desktop.
Git cheatsheet

Basic commands

For global username and useremail

  git config --global user.name "<username>"

  git config --global user.email "<email>"

Git log (To see your commit or other's commit message history)

git log

Git version

  git --version

Git intialize

  git init

Git status

  git status

Git rm (For pulling the file to untracked files)

  git rm --cached <file_name>
  git rm --cached index.html

Git add (This command adds a change in the working directory to the staging area)

  git add index.html  => For add the index.html to the staging area

  git add .           => For adding all untracked files to staging area

  git add *.html      => For adding all html files to staging area

Git push (This command is used to upload local repository content to a remote repository)

  git push -u <remote_name> <branch_name>

  git push -u origin <branch_name>

The remote name is a short-hand label for a remote repository. origin is the conventional default name for the first remote and is usually where you push to when you don't specify a remote for git. You can set up more than one remote for your local repo and you use the remote name when pushing to them.

Git checkout

  git checkout <branch_name>     => For switching branch
  git checkout -b  <branch_name> => For creating new branch

Git merge (This command will help merge to the master branch)

  git merge <branch_name>

Deleting the branch

  git branch -d <branch_name>              => For deleting local branch

  git push -d <remote_name> <branch_name> => For deleting remote branch

In most cases the remote name is origin.

"-d" can also be written as "--delete", "-D"

Git ignore

Git sees every file in your working copy as one of three things:

  1. Tracked - a file which has been previously staged or committed.
  2. Untracked - a file which has not been staged or committed.
  3. Ignored - a file which Git has been explicitly told to ignore.
  touch .gitignore

If we want something that should not be staged while adding to git, it can be done with the help of gitignore

  1. If we don't want file named "index.hml" to be staged, add this in the gitignore file.
index.html
  1. If we don't want folder named "secretFolder" to be staged, add this in the gitignore file.
/secretFolder
  1. If we don't want html files to be staged, add this in the gitignore file.
*.html

For cloning

  git clone <url>

  cd <repo_name>

  git fetch

  git checkout <branch_name>

repo is a short form of repository

For creating a repo and pushing it to remote repo

  git init

  git config --global user.name "<username>"

  git config --global user.email "<email>"

  git remote add origin <repo_url>

  git add .

  git commit -m "commit message goes here"

  git push -u origin master

For adding branch to existing repo

  git add .

  git commit -m "commit message goes here"

  git checkout -b <branch_name>

  git push -u origin <branch_name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment