Git & Github Notes

Common Git Commands
Command Description
git init Initialize a new Git repository. Git creates a hidden directory called .git that stores all the objects and refs that Git uses.
git add [file] Add changes in the specified [file] to the staging area.
git add . Add all changes in the current directory to the staging area.
git commit -m "message" Commit (save) the staged changes with a "message".
git remote add origin [url] Add a remote repository with the name "origin".
git config --global user.name "Your Name" Set your Git username.
git clone [url] Clone a repository from a remote URL.
git remote -v List all remote repositories.
git branch List all branches in the repository.
git branch -M [Branch Name] Rename the current branch to [Branch Name]
git checkout [branch] Switch to the specified branch.
git checkout -b [branch] Create and switch to a new [branch]
git push origin [branch] Push changes to the specified branch on the remote repository.
git push origin main --force Force push to GitHub to overwrite the previous pushed commit.
git pull origin [branch] Fetch and merge changes from the specified branch on the remote repository.
git status Show the status of changes in the working directory and staging area.
git reset Undo commits
git log View commit history
git merge [branch] Merge another [branch] into current one
git commit --amend Modify the last commit message or include additional changes.
git reset --soft HEAD~1 Undo the last commit but keep changes staged.
git reset --mixed HEAD~1 Undo the last commit and unstage changes.
git reset --hard HEAD~1 Undo the last commit and discard all changes (dangerous).
git restore [file] Discard changes in the working directory for a file (safe).
git restore --staged [file] Unstage a file while keeping changes in the working directory.
git revert [commit-hash] Create a new commit that reverses the effects of a previous commit. (Safe for shared branches)
git clean -fd Remove untracked files and directories. (⚠️ Use with caution)
git stash Temporarily save uncommitted changes
git stash pop Apply stashed changes and remove the stash.
git stash apply Apply stashed changes but keep the stash for reuse.

This is a shortlist of the most commonly used Git commands. For beginners, the first 16 are enough and widely used by most GitHub users.

Fork means copying someone else's repository to your own GitHub account so you can develop or contribute from your side. Later, you can send a pull request to the original owner for merging.

Note: If you want to use GitHub Pages for hosting, ensure your homepage file is named 'index.html'

♠ Interview Questions

1. What is Git and What are the advantages of using Git?

Answer: Git is a distributed version control system (VCS) that helps developers track changes in source code and collaborate effectively.

Advantages of using Git:
  • Tracks every change with detailed history.
  • Supports branching and merging for parallel development.
  • Works offline (distributed).
  • Fast and efficient performance.
  • Allows easy collaboration via platforms like GitHub or GitLab.
2. What is the difference between Git and GitHub?

Answer: Git is a local version control system, while GitHub is a cloud-based platform for hosting Git repositories.

Key Differences:
  • Git: Tracks changes in code locally on your computer.
  • GitHub: Allows you to share code online, collaborate, and manage repositories.
3. How do you create a new branch in Git?

Answer: You can create and switch to a new branch using the command below:

  • git checkout -b [new-branch-name]

Or, you can create and switch in two steps:

  • git branch [new-branch-name]
  • git checkout [new-branch-name]
4. What is a merge conflict and how do you resolve it?

Answer: A merge conflict occurs when Git cannot automatically merge changes because the same part of a file was modified differently in two branches.

To resolve it:
  • Git highlights the conflict markers in the file.
  • You manually edit the file to choose or combine changes.
  • Then, stage and commit the resolved file.
  • git add [filename]
  • git commit
5. Explain the purpose of the staging area in Git.

Answer: The staging area in Git is a place where changes are prepared before committing. It allows you to review and control exactly what will go into your next commit.

6. How do you revert a commit in Git?

Answer: You can undo changes from a specific commit using:

  • git revert <commit-hash> // creates a new commit that reverses the changes.
To completely remove a commit (use with caution):
  • git reset --hard HEAD~1
7. What is the purpose of the .gitignore file?

Answer: The .gitignore file tells Git which files or directories to ignore. It helps keep unnecessary, temporary, or sensitive files out of your repository.

Common examples:
  • node_modules/
  • .env
  • dist/
8. How can you view the commit history in Git?

Answer: You can use the following commands to view commit history:

  • git log // shows detailed commit information.
  • git log --oneline // displays a compact view with one line per commit.
  • git log --graph // shows a visual representation of the branch structure.
  • git log --oneline --graph // combines both compact view and graph for a clear summary.

Basic things of git and github

Basic process of creating github repository

Or connect your github account with vscode to use the inbuild feature

Save or commit the further changes to github repository

Or directly from vscode

How to overwrite recent commit (To avoiding creatation of unnecessary commit)

Git Best Practices