DEV Community

gaganjot singh
gaganjot singh

Posted on

Every Git Command You Need to Know

Git is a powerful tool for version control, enabling developers to manage their code with precision and collaboration. Whether you’re a beginner or a seasoned professional, mastering Git commands is essential. Here’s a comprehensive guide to every Git command you need.


1. Getting Started with Git

Before you begin, set up your Git repository:

  • git init

    Initialize a new Git repository in your project directory.

  • git clone <repository_url>

    Clone an existing repository to your local machine.


2. Tracking Changes

Git helps you track changes effectively:

  • git add <file>

    Stage a specific file for the next commit.

  • git add .

    Stage all changes in the current directory.

  • git commit -m "Your message"

    Save a snapshot of the staged changes with a descriptive message.

  • git status

    Check the status of your working directory and staging area.

  • git log

    View the commit history. Add --oneline for a concise format.


3. Branching and Merging

Work on features or fixes without affecting the main codebase:

  • git branch

    List all branches in your repository.

  • git branch <branch_name>

    Create a new branch.

  • git checkout <branch_name>

    Switch to the specified branch (or use git switch).

  • git merge <branch_name>

    Merge the changes from one branch into the current branch.

  • git branch -d <branch_name>

    Delete a branch safely. Use -D to force deletion.


4. Stashing Changes

Save your work temporarily without committing:

  • git stash

    Stash uncommitted changes.

  • git stash pop

    Apply stashed changes and remove them from the stash list.

  • git stash list

    View all stashed changes.

  • git stash drop

    Delete a specific stash.


5. Collaborating with Remote Repositories

Work with teammates using remote repositories:

  • git remote add <name> <url>

    Connect your local repository to a remote repository.

  • git pull

    Fetch and merge changes from a remote repository.

  • git push

    Push commits to the remote repository. Use -u origin <branch_name> to set up tracking.

  • git fetch

    Retrieve changes from a remote repository without merging.


6. Managing Tags

Tags are great for marking releases or milestones:

  • git tag <tag_name>

    Create a lightweight tag for the current commit.

  • git push origin <tag_name>

    Push your tag to the remote repository.

  • git tag -d <tag_name>

    Delete a tag locally.


7. Undoing Mistakes

Mistakes happen! Git lets you undo them gracefully:

  • git checkout -- <file>

    Discard changes to a file.

  • git reset <file>

    Unstage a file but keep its changes in the working directory.

  • git reset --soft HEAD~1

    Undo the last commit, keeping changes staged.

  • git reset --hard HEAD~1

    Undo the last commit and discard changes.

  • git revert <commit_hash>

    Create a new commit that undoes a specific commit.


8. Viewing and Comparing Changes

Easily track differences in your project:

  • git diff

    Show unstaged changes in your working directory.

  • git diff --staged

    Display changes that are staged for the next commit.

  • git diff <branch1> <branch2>

    Compare two branches.


9. Advanced Commands

Take your Git knowledge to the next level:

  • git blame <file>

    Show who made changes to each line in a file.

  • git reflog

    View a log of all references, including deleted ones.

  • git clean -f

    Remove untracked files.

  • git bisect

    Use binary search to find a commit that introduced a bug.


10. Configuring Git

Personalize Git to your needs:

  • git config --global user.name "Your Name"

    Set your global username.

  • git config --global user.email "you@example.com"

    Configure your global email address.

  • git config --list

    View all your Git configuration settings.


Conclusion

Git is your ultimate tool for managing code, tracking changes, and collaborating with others. From basic commands to advanced features, this guide equips you with everything you need. Bookmark this cheat sheet and refer back anytime.

Happy coding! 🚀

Top comments (0)