git config –list
git config –global [ for any git repo ]
git config –local [ for a local repo ]
What are the main areas in a git repo?
- Working Tree
- Staging Area [ Index ]
- History or Logging
The only way to track files is to stage them i.e. to add them in the staging area.
Important note about .gitignore :
- .gitignore cannot ignore files if they are already in the Staging Area. Once they are being tracked, they cannot be ignored.
How to add all untracked/tracked files in Staging Area ?
- git add .
How to see all changes per commit ?
- git log -p [ shows all changes per commit ]
How to check difference between Working Tree and the Staging Area ?
- git diff
How to check difference between Staging Area and the most recent commit [ History ] ?
- git diff –staged
How to revert to the file in Staging Area ?
- git checkout — <file> restores the file from Staging Area to the Working Tree effectively resetting all changes of the file in the Working tree. Post this command the Staging Area and the Working Tree are in Sync.
How to un-stage a file ?
- git reset HEAD <file> restores file from the last commit of <file> into the Staging area. However now the Staging Area and the History are in Sync, BUT the Working Tree and Staging Area are not. So use the command from the question above.
Retrieving a file that was added at one point but deleted later [ Restoring from an earlier commit ] ?
- git log — <file> shows when it was deleted [ if you comment well ]
- git checkout <hash> — <file> [ this will add s2 in Staging Area and the Working Dir]
- git commit -m “restore file”
Important note about Commit :
- Every commit has a 40 digit hexa-decimal SHA1 Hash
What is the .git directory ?
- .git directory has the complete git project of that working tree. If you send that it is like sending the whole git repo to someone.
What is HEAD pointer and what is detached HEAD?
- HEAD pointer normally points to a branch AND a commit simultaneously [ more specifically to the latest commit in the current checked out branch ]
- If HEAD only points to a commit BUT not a branch, this is a detached head state.
Why a 3 way merge and not a 2 way merge ?

What is git stashing?
If you have made some changes, but are not yet ready to put it in a Staging Area because you are unsure. We can stash the changes by saying:
- git stash
- git stash list -p [ To check the stashed changes ]
What is the use? If you make untracked changes and wish to switch to a new branch, git will need you to stash these changes or else it will wipe the changes off in the current branch when you move to a new one. It is better to stash them so they can be recovered later.
How to delete branches?
We can only delete merged branches. Check merged branches using –
- git branch –merged
- git branch -d <branch name>
What is a remote repo?
Github is the remote repo for the local .git repo. There can be a number of other platforms like Gitlab, Bitbucket etc. which can work as a remote repo. You can literally create a repo on another laptop and that can act as a remote repo.
The local <-> remote repo is a watered down distributed system and the users main job is to keep them in sync.
- git remote -v [ prints the full remote location of the repo and what we locally call it ]
Once you clone from a remote repo or create a remote repo, you’ll see multiple branches :
- HEAD -> master
- ddads/master
Basically HEAD -> master is a reference to a local commit, and ddads/master is the reference to the last remote commit. If they are on the same line, you are in sync.
How to merge the remote repo with the local repo?
- git merge ddads/master
This will merge the remote changes [ from the master branch of ddads] into the local repo.
- git pull [ combines git fetch and get merge into a single command ]
Best Git Practices