Skip to content

Instantly share code, notes, and snippets.

@greatsharma
Last active July 8, 2021 06:28
Show Gist options
  • Save greatsharma/2165a2855d163335730d2f49caeb1cef to your computer and use it in GitHub Desktop.
Save greatsharma/2165a2855d163335730d2f49caeb1cef to your computer and use it in GitHub Desktop.
Some Important git commands.
Resources ->
http://rogerdudler.github.io/git-guide/
https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches
* git config cmds :-
git config --global user.name "user name"
git config --global user.email "user email"
git config --global color.ui "auto"
git config --global core.editor "vim"
git config --list [show all git config]
* add sshkey to github
ssh-keygen -t rsa -b 4096 -C "mail_id"
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa
clip < ~/.ssh/id_rsa.pub
github settings > SSH and GPG keys > New SSH key > paste key
* git init
* git status
* git clone repo_url
* git remote add origin repo_url
* git remote rm origin [remove origin]
* git add file_name [add untracked file to index/stage tree
and make it tracked file or in staging area]
* git commit -m "message" [file will be added to HEAD tree, now the
file is commited to HEAD i.e., stored permanently
to the local working copy]
* git commit -a -m "message" [commit without staging]
* git checkout -- . [undo all unstaged changes]
* git push origin -u <branch_name> [push commited changes to remote repository branch]
* git push origin --delete <branch_name> [delete remote branch]
* git push [push master to origin/master]
* git remote add origin <server> [if not cloned an existing repo and want to connect
the repo to remote]
* git remote -v [gives the origin/remote]
* git log [gives all commit history]
* git log --all --decorate --decorate --graph [a-dog]
* git log --author=gaurav
* git log --name-status [see only which file have changed]
* git reflog [get log of all commits even which are lost now]
* gitk [gui for git log]
* git citool [open git-gui in ubuntu]
* git diff [changes b/w working dir & what was last changed]
* git diff --staged [changes b/w staging area and last commit]
* git diff commit_hash [changes b/w current dir and that hash_dir]
* git diff HEAD~1 [changes made in the last commit]
* git diff HEAD~2 [changes made in the last 2 commits]
* git diff <source_branch> <target_branch>
* git diff --name-only [show only changed file names]
* git checkout HEAD file_name [recover file from last commit]
* git reset --hard HEAD~1 [change head]
* git reset --hard commit_hash [reset head to the stated hash]
* git checkout -b "branch_name" [create and switch to new branch]
* git checkout branch_name [switch to branch_name]
* git checkout branch_name filename_in_branchname [if you are in branch1 and want to merge changes of file1 of branch2 then do, 'git checkout branch2 file1' from branch1]
* git branch -m <new_name> [rename the current branch]
* git branch -d branch_name [delete branch_name]
* git branch [current working branch name]
* git branch -a [list all branches]
* git merge <branch> [merge branch with working branch]
* git remote show origin [remote status]
* git fetch origin [just downloads the changes from remote to local]
* git pull [downloads the changes and merges them into current branch]
* git pull <remote> <branch> [Remote is either origin or upstream]
* if you want to drop all local changes and commits, fetch the latest
history from the remote and point local master branch at it like this -
git fetch origin
git reset --hard origin/master
* git ls-tree -r branch_name [list all files in a tree object]
* git diff <branchA>:<fileA> <branchB>:<fileB> [compare files across branch]
* origin - refers to forked repo
upstream - refers to original repo from where we forked
* git remote add upstream repo_url [add upstream to keep track of it]
* git fetch upstream
* sync upstream/master with host/master:
* git checkout master
* git fetch upstream
* git merge upstream/master
* git tag 1.0.0 first_few_characters_of_commitid_to_tag
* git push origin <tag_name>
* git push --tags [push all tags]
* git tag -d <tag_name>
* git push --delete origin tagname
* git archive branch_name -o file_name.zip [zip branch]
* git rebase <remote>/master [sync current branch with master]
* rebase pr
git merge-base my-branch edx/master
git rebase -i hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment