Git Commands and Associated Questions

Estimated reading: 3 minutes 16 views

What is Git?

Answer: Git is a distributed version control system that allows developers to track changes in source code, collaborate on projects, and maintain a full history of the codebase.

How do you initialize a Git repository?

Answer: To initialize a new Git repository, use:

				
					git init

				
			

How do you clone a remote Git repository?

Answer: To clone an existing repository from a remote location, use:

				
					git clone <repository_url>

				
			

How do you check the status of the repository?

Answer: To check the status, which shows staged, unstaged, and untracked files, use:

				
					git status

				
			

How do you add changes to the staging area?

Answer: To add a specific file:

				
					git add <file_name>

				
			

To add all changes:

				
					git add .

				
			

How do you commit changes in Git?

Answer: To commit changes with a message, use:

				
					git commit -m "Your commit message"

				
			

How do you push local changes to a remote repository?

Answer: To push changes to a specific branch on the remote repository:

				
					git push origin <branch_name>

				
			

How do you pull changes from a remote repository?

Answer: To fetch and merge changes from the remote repository:

				
					git pull origin <branch_name>

				
			

How do you create a new branch in Git?

Answer: To create and switch to a new branch:

				
					git checkout -b <branch_name>

				
			

How do you switch branches in Git?

Answer: To switch to an existing branch:

				
					git checkout <branch_name>

				
			

How do you merge branches in Git?

Answer: To merge a branch into the currently active branch:

				
					git merge <branch_name>

				
			

How do you delete a local branch in Git?

Answer: To delete a local branch:

				
					git branch -d <branch_name>

				
			

How do you delete a remote branch in Git?

Answer: To delete a remote branch:

				
					git push origin --delete <branch_name>

				
			

How do you view the commit history?

Answer: To view the commit history, use:

				
					git log

				
			

How do you view all branches in the repository?

Answer: To list all local branches:

				
					git branch

				
			

To list remote branches:

				
					git branch -r

				
			

How do you revert a commit in Git?

Answer: To revert the changes made by a commit, creating a new commit that undoes it:

				
					git revert <commit_hash>

				
			

How do you reset your repository to a previous commit?

Answer: To reset to a previous commit and discard changes:

				
					git reset --hard <commit_hash>

				
			

How do you create a tag in Git?

Answer: To create a tag for a commit:

				
					git tag <tag_name> <commit_hash>

				
			

How do you push tags to a remote repository?

Answer: To push a tag to a remote repository:

				
					git push origin <tag_name>

				
			

How do you view a list of all the tags in the repository?

Answer: To list all tags:

				
					git tag

				
			

How do you stash changes in Git?

Answer: To temporarily save changes that aren’t ready to be committed:

				
					git stash

				
			

How do you apply stashed changes?

Answer: To apply stashed changes:

				
					git stash apply

				
			

How do you drop a stash from the list of stashes?

Answer: To remove a specific stash:

				
					git stash drop <stash_id>

				
			

How do you list all the stashes?

Answer: To list all stashed changes:

				
					git stash list

				
			

How do you fetch changes from a remote repository without merging them?

Answer: To fetch changes without merging:

				
					git fetch

				
			

What is the difference between git pull and git fetch?

Answer:

  • git fetch only downloads changes from the remote repository but doesn’t merge them.
  • git pull fetches and merges the changes into the current branch.

Leave a Comment

Share this Doc

Git Commands and Associated Questions

Or copy link

CONTENTS