Git is an essential tool for web developers, whether you're working solo or collaborating on a team. Understanding the core commands can improve your workflow and help you manage your codebase effectively. In this article, weβll cover some of the must-know Git commands for web developers and explain how they can be useful in everyday tasks.
1. Basic Commands π οΈ
git init
π
Image Idea: A simple terminal screenshot showing the output of running git init
in a new project directory.
Initializes a new Git repository in your project folder. This is how you start tracking your code with Git from scratch.
git clone <repository-url>
π¦
Image Idea: A visual of cloning a repository from GitHub or GitLab, with a URL being copied and a terminal cloning process.
Clones an existing repository from a remote server to your local machine. This is how you get an existing projectβs code to your computer.
git status
π
Image Idea: Screenshot of git status
command showing changes in a project.
Shows the current state of your working directory and the staging area, including untracked or modified files.
git add <file>
β
Image Idea: A before-and-after visual of git add .
in action, showing untracked changes before and then staged changes after.
Stages a specific file for commit. You can also stage everything with git add .
.
git commit -m "message"
βοΈ
Image Idea: A clean and concise commit message example shown in the terminal after running this command.
Commits the staged changes with a meaningful message, which serves as a snapshot of your project at a specific point.
git push origin <branch-name>
π
Pushes your local commits to a remote repository. For example, git push origin main
pushes changes to the main branch.
git pull
π
Image Idea: Side-by-side terminal screenshot showing the result of git fetch
and git pull
, with clear differences in handling updates.
Fetches and integrates changes from a remote repository into your current branch. Itβs a combination of git fetch
and git merge
.
2. Branching & Merging πΏπ
git branch
π³
Lists all local branches. Branching is key for feature development without disrupting the main codebase.
git branch <branch-name>
π±
Creates a new branch. Branching allows you to work on new features or bug fixes in isolation.
git checkout <branch-name>
πΆ
Switches to the specified branch. You can also create a new branch and switch to it directly with git checkout -b <branch-name>
.
git merge <branch-name>
π
Image Idea: Diagram showing two branches being merged together visually, or a terminal example showing a successful git merge
.
Merges the specified branch into your current branch. This is often used to integrate feature branches back into the main branch after development.
git branch -d <branch-name>
ποΈ
Deletes the specified branch after it has been fully merged.
3. Viewing Changes π
git log
π
Image Idea: Terminal screenshot of git log
showing a list of commits with commit hashes, authors, and messages.
Displays the commit history for the current branch. Use this to view previous commits and see who made changes.
git diff
π§
Shows the differences between your working directory and the staging area, or between commits.
git show <commit-hash>
π
Shows the details of a specific commit, including the files changed and the commit message.
4. Undoing Changes π«
git reset <file>
β©οΈ
Removes a file from the staging area without affecting the actual file.
git reset --hard <commit-hash>
π
Resets your working directory and index to a specific commit, discarding any changes made after that commit.
git revert <commit-hash>
βͺ
Creates a new commit that reverses the changes introduced by a previous commit. This is safer than reset
because it preserves history.
5. Remote Repositories π
git remote -v
π
Lists all remotes associated with your project.
git fetch
π₯
Downloads objects and refs from another repository but does not integrate them into your working files.
git remote add <name> <url>
βπ
Adds a new remote repository. You can have multiple remotes for the same project if needed.
6. Stashing Changes π§³
git stash
π¦
Temporarily saves your uncommitted changes so you can work on something else without losing your progress.
git stash pop
π
Reapplies the most recently stashed changes and removes them from the stash list.
git stash list
ποΈ
Lists all the stashes youβve created.
7. Collaborating π€
git fetch origin
β¬οΈ
Downloads the latest changes from the remote repository without affecting your working branch.
git pull origin <branch-name>
π
Fetches and merges changes from a remote repository into your current branch.
git rebase <branch-name>
π οΈ
Image Idea: Diagram showing a simplified Git rebase process compared to merge (e.g., side-by-side with clear differences).
Reapplies your changes on top of another branch. This is often used to keep a clean project history when collaborating.
8. Tagging π·οΈ
git tag <tag-name>
π
Creates a tag at the current commit. Tags are useful for marking specific points in your history, such as releases.
git push origin <tag-name>
ππ·οΈ
Pushes a specific tag to the remote repository.
9. Cleaning Up π§Ή
git clean -f
ποΈ
Removes untracked files from the working directory. Use this with caution because it will delete files not tracked by Git.
git clean -fd
π§ΉποΈ
Removes both untracked files and directories, which can help tidy up your working directory.
Final Thoughts π‘
These commands form the backbone of any developer's Git toolkit. Whether youβre working on a solo project or contributing to a team repository, mastering these commands will make managing code smoother and more efficient. By practicing these Git commands regularly, youβll feel more confident handling different scenarios in web development.
Are there any specific commands you use frequently that didnβt make the list? Feel free to share them in the comments below! Happy coding! ππ¨βπ»π©βπ»