Git Cheat Sheet: A Quick Reference Guide

ยท

2 min read

As a software developer, mastering Git is essential for efficient version control and collaboration. Whether you're new to Git or need a quick refresher, this cheat sheet will help you navigate the most common Git commands.

Configuring Git

Before you start using Git, it's important to set your identity.

1.Configure username:

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

2.Configure email:

git config --global user.email "youremail@example.com"

Basic Git Commands

  1. Initialize a new Git repository:

     git init
    

    2.Clone a remote repository:

     git clone repository_url
    

3.Check the status of your repository:

git status

3.Add changes to the staging area:

git add filename

4.Commit changes with a message:

git commit -m "Your commit message"

5.Push changes to a remote repository:

git push

6.Pull changes from a remote repository:

git pull

Branching and Merging

1.Create a new branch:

  1.  git branch branch_name
    

    2.Switch to branch:

     git checkout branch_name
    

    3.Create and switch to a new branch:

     git checkout -b new_branch_name
    

    4.Merge changes from one branch into the current branch:

     git merge branch_name
    

    5.Delete a branch (locally):

     git branch -d branch_name
    

    6.Delete a branch (remotely):

     git push origin --delete branch_name
    

    Viewing History

    1.View the commit history:

     git log
    

    2.View a simplified commit history:

     git log --oneline
    

    3.View changes made in a specific commit:

     git show commit_hash
    

    Ignoring Files

    Create or edit a '.gitignore' file to specify files or patterns to be ignored.

    Tagging and Releases

    1.Create an annotated tag:

  1.  git tag -a tag_name -m "Tag message"
    

    2.List all tags:

git tag

Undoing Changes

1.Discard changes in the working directory:

git checkout -- filename

2.Unstage changes (move them back to the working directory):

git reset filename

3.Revert a commit:

git revert commit_hash

4.Reset to a previous commit (use with caution):

git reset --hard commit_hash

This Git cheat sheet provides a handy reference for everyday Git tasks. As you become more comfortable with Git, explore its full range of features and options to streamline your development workflow.

Remember to replace placeholders like "branch_name," "filename," and "commit_hash" with specific values relevant to your Git workflow.

Happy coding! ๐Ÿš€

ย