Git Guide: Understanding Essential Commands and Concepts

Git is a powerful version control system that helps developers manage their code efficiently. Below is a guide that walks through the most important Git commands, along with examples to clarify their usage.
1. git add: Adding Changes to Staging Area
The git add command adds your file changes to the staging area in Git. It prepares files for the next commit.
Example:
git add <file-name> # Add specific file git add . # Add all changesAfter modifying
index.html, you can rungit add index.htmlto stage the changes for the next commit.
2. git commit: Saving Changes to the Repository
git commit saves your staged changes to the local repository. Each commit represents a snapshot of your code at a particular point in time.
Example:
git commit -m "Added a new feature"This commits your staged changes with the message
"Added a new feature". It's best practice to use clear, concise commit messages.
3. git branch: Managing Branches
Branches allow you to work on different features or fixes independently without affecting the main codebase.
Example:
git branch feature-1 # Create a new branch git checkout feature-1 # Switch to the new branchThe command
git branch feature-1creates a new branch calledfeature-1.
4. git push: Pushing Changes to Remote Repository
Once your changes are committed locally, you can push them to a remote repository with git push.
Example:
git push origin feature-1This pushes the changes from your local
feature-1branch to theoriginremote repository.
5. git pull: Fetching and Merging Changes from Remote
git pull is used to fetch and merge changes from a remote repository into your current branch.
Example:
git pull origin mainThis command fetches and merges the latest changes from the
mainbranch of theoriginrepository into your local branch.
6. git remote: Managing Remote Repositories
Remote repositories allow multiple developers to collaborate. You can add, view, or remove remotes with git remote.
Example:
git remote add origin https://github.com/user/repo.git git remote -v # View remote URLsThe
git remote add origincommand sets a remote repository URL that you can push and pull changes to.
7. git config: Configuring Git Settings
git config allows you to set configuration options, such as your name and email, which are used in commits.
Example:
git config --global user.name "John Doe" git config --global user.email "john@example.com"This sets your username and email globally for all repositories.
8. Pull Request (PR): Collaboration with Code Review
A Pull Request (PR) is a GitHub feature that allows for collaboration by requesting that someone reviews and approves your changes before merging them into the main branch.
Example: In GitHub:
Push your branch to the remote repository.
Go to GitHub and create a new pull request from
feature-1tomain.Reviewers will provide feedback before the code is merged.
9. git merge: Merging Branches
git merge integrates changes from one branch into another. It preserves both branches' histories and creates a merge commit.
Example:
git checkout main git merge feature-1This merges the changes from the
feature-1branch into themainbranch.
10. git rebase: Rewriting Commit History
git rebase re-applies commits from your current branch on top of another branch, resulting in a linear history.
Example:
git checkout feature-1 git rebase mainThis reapplies all commits from
feature-1on top ofmain, making the commit history linear and avoiding a merge commit.
11. Merge Conflict: Resolving Conflicts Between Branches
Merge conflicts happen when Git cannot automatically merge changes. You will need to manually resolve the conflicts.
Example: After attempting to merge:
git merge feature-1If a conflict occurs, Git will notify you. You can resolve the conflicts in your code and then run:
git add . git commit -m "Resolved merge conflict"
12. git reset: Undoing Commits
git reset moves the HEAD of the branch back to a previous state. It can modify the commit history and working directory.
Example:
git reset --soft HEAD~1 # Undo last commit, keep changes stagedThis undoes the last commit but keeps your changes in the staging area.
13. git revert: Undoing a Commit without Rewriting History
git revert creates a new commit that undoes the changes of a previous commit, without changing the commit history.
Example:
git revert <commit-hash>This creates a new commit that reverses the changes of the specified commit.
14. git cherry-pick: Applying Specific Commits from Another Branch
git cherry-pick allows you to pick a specific commit from another branch and apply it to your current branch.
Example:
git cherry-pick <commit-hash>This applies the changes from a specific commit in one branch to your current branch.
15. git log: Viewing Commit History
The git log command displays the commit history of your repository. It helps you see the order of commits and their details.
Example:
git log git log --oneline # Compact viewThis shows the commit history. The
--onelineoption provides a summarized view, showing only the commit hash and message.
16. git stash: Temporarily Saving Changes
git stash allows you to temporarily save your changes without committing them, so you can work on something else without losing your progress.
Example:
git stash # Save changes to stash git stash apply # Reapply the changes from stash git stash drop # Remove stash after applyingAfter running
git stash, your changes will be removed from the working directory but saved in a "stash". You can later reapply those changes withgit stash apply.




