Skip to content

Instantly share code, notes, and snippets.

@chauhan-utk
Last active September 11, 2019 10:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chauhan-utk/84354cd2eb60fa8825f773fa69b7a761 to your computer and use it in GitHub Desktop.
Save chauhan-utk/84354cd2eb60fa8825f773fa69b7a761 to your computer and use it in GitHub Desktop.
Git and GitHub basic commands

Basic git commands:

  • git fetch : get remote commits to local repo
  • git pull <repo> <branch-name> e.g. git pull origin master : get the commits from remote branch to current branch
  • git branch -a : list all branches including remote branches
    • git checkout <branch-name> : switch to the branch
  • git branch -m <new-name> : rename current local branch. Check this for more information.
  • git checkout -b <new-branch> : create a new branch based on the current branch
    • git checkout -b <new-branch> <existing-branch> : create a new branch given an existing branch
  • git status : list of files tracked and not tracked
  • git add . or git add <file-names> : add files to staging area
    • git add "*.py" : add all python files to staging area
  • git push <repo> <branch-name> e.g. git push origin master : push the local changes to remote branch
  • git push -f origin master : force local updates to be pushed to remote
  • discard the local changes from working area : git checkout -- <file-name
  • git fetch git checkout origin/master -- path/to/file : checkout a particular file. See this for more information.
  • git remote show url : show the remote repo url. Check this link for more details.
  • git branch -m new-name : rename current branch. Check this for more info.

Stashing

  • git stash list : list all git stashes
  • git stash apply <number> : apply particular git stash or the recent one (0) if no number is provided
  • git stash drop <number> : drop the particular git stash or the recent one (0) if no number is provided
  • git stash clear : clear all git stash form the list
  • git stash push -m welcome_cart app/views/cart/welcome.thtml : stash particular files. Check this for more information.
  • git stash save "my_stash" : save stash with a message

Undo mistakes using git revert

There are several ways to undo a mistake, each with a different outcome. The best one is to use git revert. Check this link and this tutorial.

  • git revert HEAD - revert the immediate last commit by making exact opposite commit.
  • git revert HEAD~2 - go back two commits.
  • git revert <sha1-1>..<sha1-3> - go back to the previous commit ranges. View git branches in terminal in nice graph form - git log --graph --decorate --oneline

Check if git pull is needed: git remote -v update

Add an empty directory to the repo (useful when want to store stuff that should not be sent to remote)

  • create directory (e.g. .logs)
  • inside .logs create .gitignore and add following:
    # Ignore everything in this directory
    *
    # Except this file
    !.gitignore

Another tutorial link by GitHub to undo stuff using git.

If want to overwrite local files with remote:

git fetch --all
git reset --hard origin/master
git pull origin master

Turn on global colors

git config --global color.ui true

Add files from a remote repo

This is particularly useful when we want to copy files from a remote repo. Usually this happens when we fork from a repo, or build upon the code from the repo and there was an update in the original repo and we want to (lazily) include all the changes from the update to our code. See this link for more information.

git remote add alt <alt-machine:/path/to/repo>
git remote update
git pull alt master

If you do a git remote -v then you will see the added remote also. If you want to remove tracking for the new repo then run git remote remove <remote> e.g., git remote remove alt. After this the remote branch should have been removed. Remember to commit the changes from the remote before removing the remote repo tracking.

See files in which changes were made between two branches

git diff --stat --color master..branchname

Check this link for the above command. If want to see the individual changes in a particular file, modify the above command as:

git diff mybranch..master -- myfile.cs

Check the name of the file in which a merge conflict is present

git diff --name-only --diff-filter=U

Including large files in repo

Git showed a message to this link for large files remote storage.

@utk-ch
Copy link

utk-ch commented Sep 11, 2019

Syncing a fork with another remote: link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment