Git Essential Commands
Common Terminology & Flow:-
[untracked] → [unstaged] → [staged] → [commit]
Initial Configuration:-
Setting Name & Email Id:
git config --global user.name <your_username>git config --global user.email <your_email>Checking Name & Email Id:
git config --global user.namegit config --global user.emailEdit config file:-
git config --global --editEnable Git in a Directory:-
git initAdding File to Stage or to be Committed:-
Adding single unstage file to Stage:-
git add <filename>Adding all untracked files at once to Stage:-
git add -Agit add .Checking Git Status & Logs:-
git statusgit status -s
// see log in shortgit loggit log -p -1
// check last 1st commit loggit log -p -5
// check last 5 commit logsgit log --oneline// short log & with short log-idgit log --pretty=oneline// short log with full log-idChecking difference between last unstaged files with current unstaged files:-
git diffChecking difference between staged files with last commit:-
git diff --stagedCommitting Staged Files:-
Commit via Nano/Vi Editor:-
git commitCommit without Nano/Vi Editor:-
git commit -m "Added More Files"or
git commit --message "Added More Files"Stage & Commit all files at once:-
git commit -a -m "Skipped staging process"Git checkout & restore command:-
// helps you move files from commit/staging-area to worktree/staging-areaMatching current files & Staged with last commit:-
git checkout <filename>
// for single filegit checkout .
// for all unstaged only filesgit checkout -f
// for all staged & unstaged filesTravel back to any last commit (HEAD pointer only, it will detach HEAD pointer):-
git checkout <log-id>Reverting staged file back to unstaged:-
git restore —-staged <filename>
// for single filegit restore —-staged *
// for all filesRemoving / Deleting files :-
git rm —cached <filename>
// remove files from commit but not deletegit rm <filename>
// remove from commit & delete fileGit Ignore:-
// 1. create file with name -> .gitignore
// 2. now add filenames in .gitignore which you want to ignored by git
// 3. also add .gitignore itself in .gitignore file
// 4. when you write a filename like mylog.log in .gitignore file so git will ignored every mylog.log files present under current working directory including under sub directories.
// 5. if you want git to ignore file only where .gitignore file is present then add that filename like: /mylog.log
// 6. if you want git to ignore a sub directory then add that directory name to .gitignore file like: tmp/ Git Branches:-
Creating git branch:-
git branch <branchname>Checking branch status:-
git branchSwitching from master branch to custom branch:-
git checkout <branchname>Switching from custom branch to master branch:-
git checkout masterMerging custom branch with master branch:-
git merge <branchname>
// make sure you are at master branchCreating & Switching from master to custom branch:-
git checkout -b <branchname>Cancel / Exit merge process when conflicts occurs:-
// generally conflicts occurs when two people from different branches make changes on same file and then try to merge those branches
git merge --abortDeleting git branch:-
git branch -d <branchname>Git Remote:-
Setting SSH keys with GitHub Account:-
// 1. generating ssh keys
ssh-keygen -t rsa -b 4096 -C "example@gmail.com"
// 2. now copy ~/.ssh/id_rsa.pub key and add to your github account
// 3. add your github repository url to your local git
git remote add <alias> <http_or_ssh_url_of_your_repo>
// example:- git remote add origin git@github.com:b0ssh3r3/myrepo.git
// 4. if you want to add more urls in the alias
git remote set-url --add <alias> <http_or_ssh_url_of_your_repo>
// example:- git remote set-url --add origin git@github.com:b0ssh3r3/myrepo.git
// 5. check configured push & pull remote url status
git remote -vcheck all remote names:-
git remote showrename remote alias:-
git remote rename <old_name> <new_name>remove remote url from an alias:-
git remote set-url --delete <alias> <full_url_or_initals_of_repo>
// example: git remote set-url --delete origin git@github.com:b0ssh3r3/testrepo.gitremove a alias:-
git remote remove <url_alias>pushing local git changes & files to remote github account:-
git push -u <alias> <branchname>
// example: git push -u origin master
// -u flag stands for upstreamgit pushpulling remote git changes & files to local git:-
git pullor
git fetch
git mergeOpen source contribution:-
// 1. search repository in which you want to contribute
// 2. fork that repository, it makes a copy of that repo inyou github account
// 3. clone that repository
// 4. create a new branch
// 5. make changes you want to do
// 6. push that repository after successful changes
// 7. create pull request
// 8. wait for user to check & approve your changes and merge it to their original repository.Git revert command (undo):-
// This command creates a new commit that undoes the changes from a previous commit. This command adds new history to the project (it doesn't modify existing history).edit last commits message:-
git revert --edit <commit_id>reverts all of the changes back to a specific commit:-
git revert <commit_id>Git clean command:-
// this command is used to delete untracked files & folders from git working treeDry run:-
git clean -nTo delete untracked files forcefully:-
git clean -fTo delete untracked directories forcefully:-
git clean -f -dGit reflog command:-
Check all commit history (even done after git reset --hard) :-
git reflogCheck all HEAD pointer history:-
git reflog HEADGit update-index command:-
Tell git you want to start ignoring the changes to the file:-
git update-index --assume-unchanged <filename>When you want to start keeping track again:-
git update-index --no-assume-unchanged <filename>To check untracked / tracked file status:-
git ls-files -v
// h -> shows untracked set
// H -> shows tracked setGit ls-files command:-
// lists the files that are in the index / staging-area, along with their staging slot numbers. (It says nothing about the copies in the HEAD commit and the work-tree.)
git ls-files --stage// lists the (names of the) files that are in the work-tree, but not in the index. (It says nothing about the copies in the HEAD commit.)
git ls-files --others// lists the (names of the) files that are in the index and are different from their copies in the HEAD commit (or aren't in the HEAD commit at all).
git ls-files --modifiedGit reset command (dangerous):-
Revert your git to any last commit, it will revert back to log_id & remove all commit logs after log_id you given (but you can see that all commits using reflog command) :-
git reset --hard <log_id>Git stash command:-
// when did some changes in you working tree & you don't want to commit & also you don't want to loose that changesPut your changes to stash:-
git stashGet your stashed things back to your working tree & delete from stash:-
git stash popShow stash details:-
git stash showList all stashed items:-
git stash listGet specific stashed item back:-
git stash pop stash@{0}
// press Tab after "git stash pop"Remove specific stashed item:-
git stash drop stash@{0}Clean or Empty your stash:-
git stash clearTo apply stashed change:-
git stash apply stash@{0}
// it bring stashed item back like pop, but it won't delete that item from stash. where as pop command bring items back & delete it from stash.Creates a new branch with the latest stash, and then deletes the latest stash ( like stash pop):-
git stash branch <name> stash@{1}References:-
Git reset vs revert command:-
https://www.pixelstech.net/article/1549115148-git-reset-vs-git-revert
Open source contribution:-
https://www.dataschool.io/how-to-contribute-on-github/
How to manage multiple GitHub accounts on a single machine with SSH keys:-
https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/
Git error - Fatal: Refusing to merge unrelated histories and how to fix it:-
https://www.datree.io/resources/git-error-fatal-refusing-to-merge-unrelated-histories
Git error - push rejected:-
https://stackoverflow.com/questions/620253/git-push-rejected
https://footle.org/2013/06/26/why-is-git-rejecting-me/#:~:text=If your push is rejected,rebase
Last updated
Was this helpful?