Skip to content

Instantly share code, notes, and snippets.

@narainsagar
Last active January 19, 2017 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save narainsagar/ee70af2b015f18b7f7fe37497e5e7aea to your computer and use it in GitHub Desktop.
Save narainsagar/ee70af2b015f18b7f7fe37497e5e7aea to your computer and use it in GitHub Desktop.
Git Quick, Useful & Easy Commands.

git useful commands

files to ignore tracking

.gitignore

an empty directory needs a file for git to keep track, convention way is to create a ".gitkeep" file

.gitkeep

display the git remote/origin

cat .git/config

display gitconfig

cat .gitconfig

display where the HEAD is pointing

cat .git/HEAD

show commit rank

git shortlog -ns 

initialize git repository in the current directory

git init .

always fetch before you work and push

git fetch

git pull = git fetch + git merge

git pull

update a cloned repository

git pull REMOTE_NAME BRANCH_NAME
for example:
git pull origin master

Show the working tree status

git status

rename specific commit message

git rebase -i commit_hash

combine the last 2 commits into 1

git rebase -i HEAD~2

combine staged changes with the previous commit

git --amend

revert the specific hash/SHA commit

git revert hash82ej23

Unstage a file's changes from staging, so they're not included in your commit.

git reset HEAD file_name

rollback to previous commit

git reset --soft HEAD^

soft/mixed/hard resets have different reset results

git reset --soft/mixed/hard SHA

removes untracked files from your working directory (can’t undo)

git clean -n

reflag allows you to go back to changesets even though they are not referenced by any branch or tag

git reflog

removing the file tracking from the staging index

git rm --cached file_name

one commit before the current HEAD (all are equivalent)

HEAD^, 8c22db944^, Head~1, HEAD~

two commit before the current HEAD (all are equivalent)

HEAD^^^, Head~3

Lists the contents of a given tree object

git ls-tree HEAD
git ls-tree branch-name

git clone

repo_url can be ssh or https (see the below line examples)

i.e., https: https://github.com/username/example-repo-name.git or ssh: git@bitbucket.org:username/example-repo-name.git

clone repo into current directory

git clone repo_url

clone repository with specific branch named branch_name

git clone -b branch_name repo_url 

clone repository to some folder (extract whole into some specific folder)

git clone repo_url repo_folder_name

clone main repo and all it's submodules

git clone --recursive repo_url

git config

git configuration

git config --global user.name "user name"
git config --global user.email "user@example.com"
git config --global core.editor "vim"

display git configuration

git config --list
git config user.name
git config core.editor

List all aliases

git config --get-regexp alias

setup line ending preferences (osx)

git config --global core.autocrlf input
git config --global core.safecrlf true

(windows)

git config --global core.autocrlf true
git config --global core.safecrlf true

setup shortcuts for certain command

git config --global alias.shortcutname "command"

creating shortcut call log and show all the options in ""

git config --global alias.logg "logg --graphic --decorate --oneline --abbrev-commit --all"

git push

forcefully push to repository

git push <reponame> -f

push contents back to remote (Github) repository, in specific branch i.e., origin is the remote and feature_branch is the branch

git push origin feature_branch
git push origin BRANCH_YOU_WANT_TO_PUSH_TO

push new branch new_branch_name to remote

git push -u origin new_branch_name

push content back to GitHub repo, in specific branch

git push -u origin master

deletes the file_name off from remote repository

git push origin --delete file_name

force the local changes to remote (shouldn't do this)

git push origin --force

git remote

List existing remotes with their urls

git remote -v

change the name of an existing remote repository

git remote rename REMOTE_NAME NEW_NAME

Add a remote repository

git remote add REMOTE_NAME GIT_REPO_URL
# for example:
git remote add origin git@github.com:user/repo.git

add URL to remote Git repository

git remote add upstream git@github.com:user/repo.git

change the uri (url) of an existing remote Git repository

git remote set-url REMOTE_NAME NEW_URL
# for example:
git remote set-url origin git://new.url.here

remove all invalid/dead remote branches

git remote prune origin

git add

add all changes to staging

git add .

add specific file to staging

git add file_name

move/rename the file name and make changes to the stage index

git mv file_name dir/file_name

git commit

add comment to commit

git commit -m "modification note"

add and commit all files at the same time

git commit -am "modification note"

git show

Shows one or more objects (blobs, trees, tags and commits)

git show

git show SHA
git show HEAD

git log

show (branches, remotes, tags, etc)

git log --branches --remotes --tags --graph --oneline --decorate

show commit log

git log

show last 3 commits

git log -3

show logs in oneliners

git log branch_name --oneline -5

list commits in current branch

git log --pretty=oneline

show all branches in graphical representation

git log --graph --oneline --all --decorate

log on remote repository

git log --oneline -5 origin/master

git log display & filter options

git log --oneline
git log --format=oneline
git log --format=oneline HEAD`3
git log --format=short, medium, full, fuller, email, raw
git log --graph (show you the trees of commits in graph)
git log --oneline --graph --all --decorate
git log --since="2012-06-20"
git log --before="2012-12-12"
git log --since=2.weeks --until=3.days
git log --author="scott"
git log --grpe="temp" (grab all commit msg that has the word "temp" in it)
git log 812yfs..09jfe (grab log between two commit ranges)
git log o87423..index.html (grab all changes in the "index" file since a commit)
git log -p o87423..index.html (grab all changes in the "index" file since a commit)
git log --status --summary

git diff

show difference in modified files yet "added" to staging

git diff

show difference in modified files in staging

git diff --staged
git diff --cached

show difference files changed between two commits

git diff --name-only SHA1 SHA2
or
git diff --name-only HEAD HEAD^

returns all the difference between that commit and current directory

git diff 02er2 index.html 

shows difference between two point of commits in time

git diff 8n9ufe..09faes

show difference between one commit and current HEAD)

git diff o8yfo3..HEAD
git diff --stat --summary 821li..HEAD

ignore changes in white spaces

git diff -b 

ignore changes in ALL spaces

git diff -w

compare master branch and new_branch_name

git diff master..new_branch_name

show in one line in color compare master branch and new_branch_name

git diff --color-words master..new_branch_name

compare master from remote to local

git diff --oneline origin/master..master

git branch

shows what branch you're on

git branch

creating a branch name new_branch_name

git branch new_branch_name

show the branch that has all merged commit under a branch

git branch --merged

renaming branch name from old_branch_name to new_branch_name

git branch -m old_branch_name new_branch_name

delete all local branches that are merged

git branch -d $(git branch --merged)

delete local branch named delete_branch_name

git branch -d delete_branch_name

FORCE delete local branch named delete_branch_name

git branch -D delete_branch_name

show your branch on remote repository

git branch -r

show your branches on both remote repository and local repository

git branch -a

delete a remote branch named branch_name on Github

git push :branch_name
# OR
git push origin :branch_name

Delete a remote branch

git checkout

to switch the head to a branch named new_branch_name

git checkout new_branch_name

create and switch to a new branch named new_branch_name

git checkout -b new_branch_name

creating a branch off remote and tracking

git checkout -b branch_name origin/branch_name

Unmodify a modified file

git checkout -- file_name

drop file and replace with version in previous commit

git checkout file_name

grab file at a specific hash/SHA commit

git checkout hash9834ef2 —- dir/file_name

grab file dir/filename and checkout to master

git checkout master -- dir/filename

git merge

merges branch you are on i.e., master and branch i.e., branch_name you were working on (it merges branch_name into master)

git merge branch_name
git merge BRANCH_YOU_WERE_WORKING_ON

merge with no fastforward

git merge --no-ff_branch

merge with only fastforward (if not possible, abort)

git merge --ff-only_branch

git stash

saving changes without commiting

git stash save "saved stash description"

show a list in the stash

git stash list

get the stash by calling that number

git stash show -p stash@{1}

pull from the stash and apply to the working directory, leaving a copy

git stash apply

pull from the stash and apply to the working directory, removing copy from stash

git stash pop

deleting the specific item in stash

git stash drop stash@{1}

clearing everything in stash

git stash clear

git tag

list tags

git tag

create new tag with message

git tag -a v1.4 -m 'my version 1.4'

see commits in a tag

git show v1.4

push a particular tag v1.4 to remote

git push origin v1.4

push all the tags to remote

git push origin --tags

checkout to the git tag v1.4

git checkout /tags/v1.4

delete an existing tag v1.4

git tag -d v1.4
git push origin :refs/tags/v1.4

rename a tag

git tag new_tag old_tag

git submodule

update all submodules

git submodule foreach git pull origin master

remove folder/module from git archive and add submodule from git repo

git rm -r --cached LOCAL_FOLDER_SUBMODULE
git submodule add GIT_REPO_URL LOCAL_FOLDER

remove all untracked files/folder

git clean -d -f -f

add submodule

git submodule add REPO_URL LOCAL_FOLDER/REPO_FOLDER

refresh all submodules

git submodule foreach git pull 

Resources:

https://www.atlassian.com/
http://git-scm.com/docs
http://gitimmersion.com/
https://training.github.com/kit/downloads/github-git-cheat-sheet.pdf
https://www.atlassian.com/git/tutorials/rewriting-history/git-reflog

good resource about submodules: http://longair.net/blog/2010/06/02/git-submodules-explained/

Keeping a clean GitHub fork: http://blog.evan.pro/keeping-a-clean-github-fork-part-1

Properly excluding IDE-specific files with Git: http://blog.evan.pro/properly-excluding-ide-specific-files-with-git

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