Here's a detailed summary of common Git commands, compiled with hands-on examples and descriptions to help you understand and use Git effectively in your development workflow.
Git: Essential Commands and Usage Guide
Git is a powerful version control system used by developers to manage and track changes in their codebase. Below is a compilation of common Git commands along with practical examples and step-by-step descriptions.
1. Initializing a Repository
Command:
git init
Description:
Initializes a new Git repository in the current directory. This creates a .git
directory where Git stores all its tracking data.
Example:
mkdir my-project
cd my-project
git init
Explanation:
- Create a new directory for your project.
- Navigate into the directory.
- Initialize a Git repository in this directory.
2. Cloning a Repository
Command:
git clone <repository_url>
Description:
Creates a copy of an existing Git repository. This command clones the repository from a remote server to your local machine.
Example:
git clone https://github.com/user/repo.git
Explanation:
- Provide the URL of the remote repository.
- Git copies all the files and commits from the remote repository to your local machine.
3. Checking Repository Status
Command:
git status
Description:
Displays the state of the working directory and staging area. It shows changes that have been staged, changes that are not staged, and files that are not being tracked.
Example:
git status
Explanation:
- Run the command to see which files have been modified, added, or deleted.
4. Adding Changes to Staging Area
Command:
git add <file>
Description:
Adds changes in the specified file to the staging area. This prepares the file for the next commit.
Example:
git add index.php
Explanation:
- Use this command to stage changes in
index.php
for committing.
To add all changes:
git add .
5. Committing Changes
Command:
git commit -m "Commit message"
Description:
Records the changes made to the files in the repository along with a commit message describing the changes.
Example:
git commit -m "Add user login functionality"
Explanation:
- Stage the changes first.
- Commit the staged changes with a descriptive message.
6. Viewing Commit History
Command:
git log
Description:
Shows the commit history of the repository, including commit IDs, author names, dates, and commit messages.
Example:
git log
Explanation:
- Run this command to review the history of commits made to the repository.
7. Creating a New Branch
Command:
git branch <branch_name>
Description:
Creates a new branch in the repository. Branches allow you to work on different features or bug fixes simultaneously.
Example:
git branch feature-branch
Explanation:
- Create a new branch called
feature-branch
.
8. Switching Branches
Command:
git checkout <branch_name>
Description:
Switches to the specified branch. This command updates your working directory to reflect the state of the branch you switch to.
Example:
git checkout feature-branch
Explanation:
- Switch to the
feature-branch
to work on it.
Note: In Git versions 2.23 and later, you can use:
git switch <branch_name>
9. Merging Branches
Command:
git merge <branch_name>
Description:
Merges the specified branch into the current branch. This integrates changes from one branch into another.
Example:
git checkout main
git merge feature-branch
Explanation:
- Switch to the branch you want to merge changes into (e.g.,
main
). - Merge the changes from
feature-branch
intomain
.
10. Pushing Changes to Remote Repository
Command:
git push origin <branch_name>
Description:
Uploads the local branch and its commits to the remote repository.
Example:
git push origin feature-branch
Explanation:
- Push the
feature-branch
to the remote repository.
11. Pulling Changes from Remote Repository
Command:
git pull
Description:
Fetches and integrates changes from the remote repository into your current branch.
Example:
git pull
Explanation:
- Update your local branch with changes from the remote repository.
12. Removing Files from Staging Area
Command:
git reset <file>
Description:
Unstages the specified file, but keeps the changes in the working directory.
Example:
git reset index.php
Explanation:
- Unstage
index.php
if it was added to the staging area but you don't want to commit it yet.
13. Deleting a Branch
Command:
git branch -d <branch_name>
Description:
Deletes the specified branch from the local repository. Use -D
to force delete.
Example:
git branch -d feature-branch
Explanation:
- Delete
feature-branch
after it has been merged or is no longer needed.
14. Stashing Changes
Command:
git stash
Description:
Temporarily saves changes in a stash so you can work on something else. Apply the stash later with git stash apply
.
Example:
git stash
Explanation:
- Save your changes temporarily if you need to switch tasks.
15. Viewing Stash List
Command:
git stash list
Description:
Displays a list of all stashed changes.
Example:
git stash list
Explanation:
- Check the list of stashed changes to decide which to apply or drop.
16. Applying Stashed Changes
Command:
git stash apply
Description:
Applies the most recent stash to your working directory.
Example:
git stash apply
Explanation:
- Retrieve your stashed changes and apply them back to your working directory.
17. Removing a Stash
Command:
git stash drop
Description:
Removes a specific stash from the list of stashed changes.
Example:
git stash drop stash@{0}
Explanation:
- Remove the specified stash from the stash list.
18. Reverting a Commit
Command:
git revert <commit_id>
Description:
Creates a new commit that undoes the changes from a specified commit.
Example:
git revert a1b2c3d
Explanation:
- Create a new commit that undoes the changes introduced by
a1b2c3d
.
Conclusion
Understanding and using these common Git commands will help you manage your codebase effectively and collaborate efficiently with others. This guide provides a solid foundation for working with Git in your development projects. Feel free to adapt and expand these commands based on your specific workflow and requirements.