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.name
git config --global user.email

Edit config file:-

git config --global --edit

Enable Git in a Directory:-

git init

Adding 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 -A
git add .

Checking Git Status & Logs:-

git status
git status -s
// see log in short
git log
git log -p -1
// check last 1st commit log
git log -p -5
// check last 5 commit logs
git log --oneline// short log &  with short log-id
git log --pretty=oneline// short log with full log-id

Checking difference between last unstaged files with current unstaged files:-

git diff

Checking difference between staged files with last commit:-

git diff --staged

Committing Staged Files:-

Commit via Nano/Vi Editor:-

git commit

Commit 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-area

Matching current files & Staged with last commit:-

git checkout <filename>
// for single file
git checkout .
// for all unstaged only files
git checkout -f
// for all staged & unstaged files

Travel 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 file
git restore —-staged *
// for all files

Removing / Deleting files :-

git rm —cached <filename>
// remove files from commit but not delete
git rm <filename>
// remove from commit & delete file

Git 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 branch

Switching from master branch to custom branch:-

git checkout <branchname>

Switching from custom branch to master branch:-

git checkout master

Merging custom branch with master branch:-

git merge <branchname>
// make sure you are at master branch

Creating & 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 --abort

Deleting 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 -v

check all remote names:-

git remote show

rename 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.git

remove 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 upstream
git push

pulling remote git changes & files to local git:-

git pull

or

git fetch
git merge

Open 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 tree

Dry run:-

git clean -n

To delete untracked files forcefully:-

git clean -f

To delete untracked directories forcefully:-

git clean -f -d

Git reflog command:-

Check all commit history (even done after git reset --hard) :-

git reflog

Check all HEAD pointer history:-

git reflog HEAD

Git 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 set

Git 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 --modified

Git 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 changes

Put your changes to stash:-

git stash

Get your stashed things back to your working tree & delete from stash:-

git stash pop

Show stash details:-

git stash show

List all stashed items:-

git stash list

Get 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 clear

To 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

https://stackoverflow.com/questions/27032850/what-is-the-difference-between-git-reset-and-git-revert/27032988

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