Git has become an indispensable tool for developers, enabling efficient version control and collaboration. This guide will walk you through the most crucial Git commands that every developer should know, helping you streamline your workflow and become more proficient in managing your projects.
- Initializing a Repository
To start using Git in a project, you first need to initialize a repository: Contribute to open source
git init
This command creates a new .git subdirectory in your current working directory, which houses all of Git’s necessary metadata for the new repository.
- Cloning a Repository
To create a local copy of an existing remote repository: Github repositories for programmers
git clone https://github.com/username/repository-name.git
This command downloads the entire repository and sets up a remote named “origin” pointing to the cloned repository’s URL.
- Checking Status
To see the current state of your working directory and staging area:
git status
This command shows which files have been modified, which are staged for commit, and which are untracked.
- Adding Changes
To stage changes for commit:
git add filename.txt # Stage a specific file
git add . # Stage all changes in the current directory
git add -A # Stage all changes in the entire working tree
- Committing Changes
To create a new commit with the staged changes:
git commit -m "Your descriptive commit message here"
For a more detailed commit message, omit the -m flag to open your default text editor.
- Viewing Commit History
To see a log of all commits:
git log
For a more concise view:
git log --oneline
- Branching
To create a new branch:
git branch new-feature-branch
To switch to a branch:
git checkout new-feature-branch
Or, to create and switch to a new branch in one command:
git checkout -b new-feature-branch
- Merging
To merge changes from one branch into your current branch:
git merge source-branch
- Pulling Changes
To fetch and merge changes from a remote repository:
git pull origin main
This command is equivalent to running git fetch followed by git merge.
- Pushing Changes
To send your local commits to a remote repository:
git push origin branch-name
- Stashing Changes
To temporarily store modified, tracked files:
git stash
To apply the most recently stashed changes:
git stash pop
- Viewing Differences
To see unstaged changes:
git diff
To see staged changes:
git diff --staged
- Reverting Changes
To undo the last commit, creating a new commit with the reversed changes:
git revert HEAD
- Resetting
To unstage changes:
git reset filename.txt
To discard all local changes and reset to the last commit:
git reset --hard HEAD
- Configuring Git
To set your username and email globally:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Advanced Git Techniques
- Interactive Rebase
To modify multiple commits:
git rebase -i HEAD~3 # Interactively rebase the last 3 commits
- Cherry-picking
To apply a specific commit from another branch:
git cherry-pick commit-hash
- Reflog
To view a log of all Git operations:
git reflog
This command is invaluable for recovering lost commits or branches.
- Submodules
To add a Git repository as a subdirectory of another repository:
git submodule add https://github.com/username/repo-name.git
- Git Hooks
Git hooks are scripts that run automatically when certain Git events occur. They’re stored in the .git/hooks directory of your repository. For example, to create a pre-commit hook that runs tests before allowing a commit:
#!/bin/sh
# .git/hooks/pre-commit
npm test
# $? stores exit value of the last command
if [ $? -ne 0 ]; then
echo "Tests must pass before commit!"
exit 1
fi
Conclusion
Mastering these Git commands will significantly enhance your development workflow. Remember, Git is a powerful tool with many more features and commands than what we’ve covered here. As you become more comfortable with these essentials, don’t hesitate to explore Git’s more advanced functionalities to further optimize your version control process.
Practice these commands regularly, and soon they’ll become second nature, allowing you to focus more on writing great code and less on managing your repository. Happy coding!