git
Git is a version control tool used by Drupal.
It is recommended you learn git separately, but here are some commonly used commands:
Basic git commands
git add
- Adds files to staging area.git commit
- Commits files to repo.-a
: Adds all unstaged files.-m
: Sets commit message from command line.--amend
: Amends the most recent commit message.
git push
- Pushes local repo to a remote repo.git pull
- Incorporates changes from a remote repository into the current branch.git clone
- Clones a remote repo to your local system.git checkout
- Switch to another branch-b
: Create a new branch prior to switching
git rebase
- Changes the history of a commit relative to another commit.git merge
- Merges one branch into another.git reset
- Resets HEAD to specified state.git clean
- Removes unstaged files from working tree.git revert
- Reverts changes made in a commit.git log
- Show commit logs.
Advanced git commands
git cherry-pick
- Applies changes introduced by another commit (useful across branches).git bisect
- Finds commit that introduced bug/functionality change using binary search which is O(lg n).git grep
- Find lines/files matching pattern within repo.git reflog
- Manage reflog information.git gc
- Performs garbage collection on local repository.
Additional Resources
Questions
What is the purpose of using the "git rebase" command in Git, and how does it differ from git merge in terms of handling the commit history of a branch?
The "git rebase" command integrates changes by moving the base of your branch onto another branch’s latest commit, creating a linear, cleaner commit history without merge commits. Unlike git merge, which preserves the branch structure with a merge commit, git rebase rewrites history, which is useful for tidiness but requires caution on shared branches.
On the other hand, rebasing is like saying, “I want to base my changes on what everybody has already done.” (Only for local commits, not for public/pushed).
What does the --amend flag do when used with the git commit command, and in what scenarios might it be useful?
The --amend flag allows you to modify the most recent commit by adding new changes or updating the commit message without creating a new commit. It’s useful for fixing small mistakes, like typos or missed files, immediately after committing.
How does the "git bisect" command help in debugging, and what are the basic steps to use it to identify a problematic commit?
The "git bisect" command helps identify the commit that introduced a bug by performing a binary search through your commit history. You mark a "bad" commit (where the bug exists) and a "good" commit (before the bug), and git bisect checks commits in between. For each commit it stops at, you test if the bug is present, marking it as "good" or "bad." Git then narrows down until it finds the exact commit that introduced the issue. This method is efficient for pinpointing bugs in large histories.
Usage:
git bisect start
git bisect bad
git bisect good <commit-hash>
git bisect reset