Git remains the cornerstone of version control for millions of developers worldwide.While its core principles are steadfast, best practices and common workflows evolve.This article revisits the most crucial Git commands, offering up-to-date examples and explaining their relevance in today’s development landscape, complete with illustrative images.Contents1.
git init ️2.git clone 3.git add 4.
git commit 5.git status 6.git log 7.
git branch 8.git checkout ️ (and git switch)9.git merge 10.
git rebase 11.git push ️12.git pull ️13.
git fetch 14.git remote 15.git stash 16.
git tag ️17.git revert 18.git reset ️19.
git config ️20.git clean 1.git init ️Purpose: Initializes a new Git repository.
This is the first step for any new project you want to track with Git.Example: BashMore Read SMEs Are Discovering The Beauty Of All-In-One Data Reporting Platforms Grasping The Cutting Edge Technology Behind Data Recovery Tools How To Cultivate Data-Driven Decision-Making In Your Workplace The UK AI Tech Sector Benefits from Immigration Key Data Trends And Forecasts In The Energy Sector mkdir my-new-project cd my-new-project git init This creates a hidden .git directory, which stores all the necessary repository files.2.git clone Purpose: Creates a local copy of an existing remote repository.Essential for joining an existing project.Example: Bashgit clone https://github.com/user/repo-name.git This command downloads the entire project history and sets up tracking for the remote repository.3.
git add Purpose: Stages changes for the next commit.It tells Git which modified files you want to include in your next snapshot.Example:Bash git add index.html # Add a specific file git add js/ # Add all files in a directory git add .# Add all changes in the current directory Using git add .
is common but be mindful to review changes with git status first.4.git commit Purpose: Records the staged changes permanently into the repository’s history.Each commit is a snapshot of your project at a specific point in time.Example:Bashgit commit -m "feat: Add user authentication module" The -m flag provides a commit message.
Modern best practice recommends descriptive and conventional commit messages (e.g., “feat:”, “fix:”, “docs:”) for better project history readability.5.git status Purpose: Shows the current state of the working directory and the staging area.
It tells you which changes are staged, unstaged, and untracked.Example:Bashgit status This command is your best friend for understanding what’s going on in your repository before committing.6.git log Purpose: Displays the commit history.You can see who made changes, when, and what the commit message was.
Example:Bashgit log # Show full history git log --oneline # Show a condensed one-line summary per commit git log --graph --oneline --all # Visualize branch history with a graph git log is incredibly versatile for exploring your project’s past.7.git branch Purpose: Manages branches.Branches are independent lines of development.
Example:Bashgit branch # List all local branches git branch feature/new-design # Create a new branch git branch -d feature/old-feature # Delete a local branch (after merging) Feature branching is a standard workflow, keeping development isolated until ready for integration.8.git checkout ️ (and git switch)Purpose: Switches between branches or restores files.While checkout is still widely used, Git introduced git switch and git restore in newer versions to clarify intent.
Example (using git switch for branches):Bashgit switch feature/new-design # Switch to an existing branch git switch -c bugfix/login-error # Create and switch to a new branch Example (using git restore for files):Bashgit restore index.html # Discard changes in working directory git restore --staged index.html # Unstage changes Recommendation: Use git switch for changing branches and git restore for managing file states, reserving git checkout for older scripts or specific advanced use cases.9.git merge Purpose: Integrates changes from one branch into another.Example:Bashgit switch main git merge feature/new-design This command brings the history of feature/new-design into main.Git will attempt a fast-forward merge if possible, or create a merge commit if there are divergent histories.10.
git rebase Purpose: Rewrites commit history.It moves or combines a sequence of commits to a new base commit.Useful for maintaining a linear project history.Example:Bashgit switch feature/new-design git rebase main This takes your feature/new-design commits and reapplies them on top of the latest main branch.
It’s often used to keep feature branches up-to-date and create clean merge histories.Caution: Do not rebase branches that have been pushed to a shared remote repository, as it rewrites history and can cause conflicts for collaborators.11.git push ️Purpose: Uploads local branch commits to a remote repository.Example:Bashgit push origin main # Push the main branch to 'origin' remote git push -u origin feature/new-design # Set upstream and push for the first time The -u (or --set-upstream) flag is crucial when pushing a new local branch, as it links your local branch to its remote counterpart.12.
git pull ️Purpose: Fetches changes from a remote repository and integrates them into the current local branch.It’s essentially git fetch followed by git merge.Example:Bashgit pull origin main Always git pull before starting new work or pushing changes to ensure you have the latest code.13.git fetch Purpose: Downloads objects and refs from another repository.
It retrieves all the latest changes from the remote without merging them into your current working branch.Example:Bashgit fetch origin This allows you to see what’s new on the remote (e.g., origin/main) without modifying your local branches.14.git remote Purpose: Manages the set of tracked repositories (“remotes”).Example:Bashgit remote -v # List existing remotes git remote add upstream https://github.com/fork/original-repo.git # Add a new remote git remote remove upstream # Remove a remote Often used in forking workflows to track the original project’s repository.15.git stash Purpose: Temporarily saves changes that are not ready to be committed, allowing you to switch contexts (e.g., branches) without committing incomplete work.Example:Bashgit stash save "WIP: login component" # Stash with a message git stash list # View stashed changes git stash pop # Apply the most recent stash and remove it git stash apply stash@{1} # Apply a specific stash without removing it An invaluable command for context switching and handling interruptions.16.
git tag ️Purpose: Marks specific points in history as important, usually used for release versions (e.g., v1.0, v2.0-beta).Example:Bashgit tag v1.0.0 # Create a lightweight tag git tag -a v1.0.0 -m "Release v1.0.0" # Create an annotated tag (recommended) git push origin v1.0.0 # Push tags to remote Annotated tags include a message, tagger, and date, and are generally preferred for official releases.17.git revert Purpose: Undoes a previous commit by creating a new commit that reverses the changes.It’s a safe way to undo changes in shared history, as it doesn’t rewrite existing commits.Example:Bashgit revert HEAD~1 # Revert the second to last commit git revert <commit-hash> Use git revert when you need to undo changes that have already been pushed to a shared repository.18.
git reset ️Purpose: Resets the HEAD pointer to a specified state.This command is powerful and can be used to unstage changes, undo commits, or remove files from the index.Example:Bashgit reset HEAD^ # Uncommit the last commit, keep changes (default --mixed) git reset --soft HEAD~1 # Uncommit, keep changes staged git reset --hard <commit-hash> # Discard all changes up to the specified commit (DANGEROUS!) git reset --hard should be used with extreme caution, as it permanently discards local changes.19.git config ️Purpose: Gets and sets repository or global options.
Used to configure your Git environment.Example:Bashgit config --global user.name "Your Name" git config --global user.email "[email protected]" git config --list # List all configurations Setting your name and email globally is usually one of the first things you do after installing Git.20.git clean Purpose: Removes untracked files from your working directory.Useful for cleaning up a build environment or starting with a fresh slate.Example:Bashgit clean -n # Show what would be removed (dry run) git clean -f # Remove untracked files (use with caution!) git clean -df # Remove untracked files and untracked directories Always use git clean -n first to avoid accidentally deleting important files.This updated guide provides a solid foundation for using Git effectively in modern development workflows.
Mastering these commands will empower you to manage your projects with confidence and collaborate seamlessly with your team.
Read More