Git and Version Control
Topic: Git and version control.
Concepts
Section titled “Concepts”- Git repository
- staging area (
git add) - commit, commit hash, commit log (commit history)
- branch
- checkout
- remote
- .git folder
- .gitignore file
git configvsgit config --global- rebase, merge
- PR (Pull Request)
git push,git pull
Questions
Section titled “Questions”- How can you obtain a copy of a Git repository from GitHub, including its full history, on another computer?
Answer
You could create a new repository on the other computer, link it to the remote repository on GitHub, and run
git pull. Another option is to usegit clone, which also automatically checks out the correct default branch.
- How can you create a Pull Request using Git (not GitHub)?
Answer
You cannot. The concept of a Pull Request does not exist in Git.
- You created a branch for feature1 based on the main branch and worked on it.
Then you created a branch for feature2, also based on the main branch, and worked on it.
You opened a PR and merged feature1 into the main branch. You then updated your local main branch with
git pull. How should you now correctly merge feature2 into the main branch?
Answer
You can open a PR directly from feature2 into the main branch. If there are no conflicts, the PR can be merged.If there are conflicts, the best approach is to run
git rebase masterwhile on the feature2 branch, resolve the conflicts locally in the affected files, and usegit rebase --continueafter resolving them.Then update the branch on GitHub using
git push --forceorgit push --force-with-lease(--forceis needed because rebasing rewrites the branch’s commit history). The PR will update automatically and will no longer show conflicts (a PR is always relative to the branch’s current state, not its initial state).