Sunday, 27 August 2023

Git Commands

Question: Configure Git
Answer:
git config --global user.name "Your Name"
git config --global user.email "[your.email@example.com](mailto: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 
Question: Check the status of your working directory and staging area
Answer:
git status
Question: Add changes to the staging area
Answer:
git add   # 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 
Question: Pull changes from a remote repository
Answer:
git pull origin 
Question: Create a new branch
Answer:
git branch 
Question: Switch to an existing branch
Answer:
git checkout 
Question: Create a new branch and switch to it in one command
Answer:
git checkout -b 
Question: Merge changes from one branch to another
Answer:
git merge 
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  
Question: Discard changes in the working directory
Answer:
git checkout -- 
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 push -m "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  
Question: Delete a branch
Answer:
git branch -d 
Question: Show the remote repositories
Answer:
git remote -v
Question: Add a remote repository
Answer:
git remote add  
Question: Change the remote repository URL
Answer:
git remote set-url  
Question: Push to a specific branch on the remote repository
Answer:
git push  :
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  -m "Tag message" 
Question: List all tags in the repository
Answer:
git tag
Question: Show the details of a specific tag
Answer:
git show 
Question: Revert changes from a specific commit
Answer:
git revert 
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 
Question: Create a new branch from a specific commit
Answer:
git branch  
Question: List all branches (local and remote)
Answer:
git branch -a
Question: Show the difference between two commits
Answer:
git diff  
Question: Show a summary of the changes introduced by a commit
Answer:
git show 
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 
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  
Question: Remove files from the staging area but keep the changes in the working directory
Answer:
git reset 
Question: Remove files from the staging area but keep them in the working directory
Answer:
git rm --cached 
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 the necessary changes.

Step 2: After editing the file, use the git add command to stage the resolved content.

Step 3: The final step is to create a new commit with the git commit command.

Step 4: Git creates a merge commit to finalize the merge when a merge requires a merge commit.

Git Commands to Resolve Conflicts:

1. git log --merge
The git log --merge command helps identify commits that are relevant to the merge conflict.

2. git diff
The git diff command helps identify differences between repository states or files.

3. git checkout
The git checkout command can be used to restore changes to files or switch branches.

4. git reset --mixed
The git reset --mixed command resets the staging area while keeping changes in the working directory.

5. git merge --abort
The git merge --abort command exits the merge process and attempts to restore the state before the merge began.

6. git reset
The git reset command can be used during a merge conflict to reset the index and move the current branch to another state, depending on the specified options.

No comments:

Post a Comment

Featured

Software Testing

Question: What is software testing, and why is it important?  Answer: Software testing is the process of evaluating a software application ...

popular