Question: Configure Git
Answer: git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Question: Create a new Git repository
Answer: git init
Question: Clone a remote repository to your local machine
Answer: git clone <repository_url>
Question: Check the status of your working directory and staging area
Answer: git status
Question: Add changes to the staging area
Answer: git add <file_name> # To stage a specific file
git add . # To stage all changes
Question: Commit changes to the repository with a descriptive message
Answer: git commit -m "Your commit message here"
Question: Push changes to a remote repository
Answer:
git push origin <branch_name>
Question: Pull changes from a remote repository
Answer:
git pull origin <branch_name>
Question: Create a new branch
Answer:
git branch <branch_name>
Question: Switch to an existing branch
Answer:
git checkout <branch_name>
Question: Create a new branch and switch to it in one command
Answer:
git checkout -b <branch_name>
Question: Merge changes from one branch to another
Answer:
git merge <source_branch>
Question: View the commit history
Answer:
git log
Question: View the difference between the working directory and the last commit
Answer:
git diff
Question: View the difference between two branches
Answer:
git diff <branch_name> <another_branch_name>
Question: Discard changes in the working directory
Answer:
git checkout -- <file_name>
Question: Discard changes in the staging area and working directory
Answer:
git reset --hard HEAD
Question: Undo the last commit and move changes to the staging area
Answer:
git reset --soft HEAD~1
Question: Create and apply a stash to save and temporarily remove changes
Answer:
git stash save "Your stash message"
git stash apply
Question: Fetch changes from a remote repository without merging
Answer:
git fetch
Question: Rename a file and stage the change
Answer:
git mv <old_file_name> <new_file_name>
Question: Delete a branch
Answer:
git branch -d <branch_name>
Question: Show the remote repositories
Answer:
git remote -v
Question: Add a remote repository
Answer:
git remote add <remote_name> <repository_url>
Question: Change the remote repository URL
Answer:
git remote set-url <remote_name> <new_repository_url>
Question: Push to a specific branch on the remote repository
Answer:
git push <remote_name> <local_branch_name>:<remote_branch_name>
Question: Pull changes from a remote repository and automatically merge
Answer:
git pull
Question: Create an annotated tag for a specific commit
Answer:
git tag -a <tag_name> -m "Tag message" <commit_sha>
Question: List all tags in the repository
Answer:
git tag
Question: Show the details of a specific tag
Answer:
git show <tag_name>
Question: Revert changes from a specific commit
Answer:
git revert <commit_sha>
Question: Change the last commit message
Answer:
git commit --amend -m "New commit message"
Question: Move the HEAD to a specific commit
Answer:
git reset <commit_sha>
Question: Create a new branch from a specific commit
Answer:
git branch <new_branch_name> <commit_sha>
Question: List all branches (local and remote)
Answer:
git branch -a
Question: Show the difference between two commits
Answer:
git diff <commit_sha_1> <commit_sha_2>
Question: Show a summary of the changes introduced by a commit
Answer:
git show <commit_sha>
Question: Move changes from the working directory to the staging area
Answer:
git add -u
Question: Delete a file from the working directory and stage the change
Answer:
git rm <file_name>
Question: Create an empty commit (useful for triggering hooks)
Answer:
git commit --allow-empty -m "Empty commit"
Question: Show the configuration settings
Answer:
git config --list
Question: Edit the Git configuration file
Answer:
git config --global --edit
Question: Show the Git version
Answer:
git --version
Question: Show the working tree status in short format
Answer:
git status -s
Question: Show the branch name you are currently on
Answer:
git rev-parse --abbrev-ref HEAD
Question: Show the changes made in the last commit
Answer:
git show HEAD
Question: Rename a branch
Answer:
git branch -m <old_branch_name> <new_branch_name>
Question: Remove files from the staging area but keep the changes in the working directory
Answer:
git reset <file_name>
Question: Remove files from the staging area and the working directory
Answer:
git rm --cached <file_name>
Question: Undo the last commit and keep the changes in the working directory
Answer:
git reset HEAD~1
Question: How to Resolve Merge Conflicts in Git
Answer:
Step 1: The easiest way to resolve a conflicted file is to open it and make any necessary changes.
Step 2: After editing the file, we can use the git add a command to stage the new merged content.
Step 3: The final step is to create a new commit with the help of the git commit command.
Step 4: Git will create a new merge commit to finalize the merge.
Git Commands to Resolve Conflicts
1. git log --merge
The git log --merge command helps to produce the list of commits that are causing the conflict.
2. git diff
The git diff command helps to identify the differences between the states repositories or files.
3. git checkout
The git checkout command is used to undo the changes made to the file, or for changing branches.
4. git reset --mixed
The git reset --mixed command is used to undo changes to the working directory and staging area.
5. git merge --abort
The git merge --abort command helps in exiting the merge process and returning back to the state before the merging began.
6. git reset
The git reset command is used at the time of merge conflict to reset the conflicted files to their original state.
Comments
Post a Comment