DEV Community

Cover image for Git Mastery: Beginner’s Guide + Cheat Sheet
Paulo Lima
Paulo Lima

Posted on

Git Mastery: Beginner’s Guide + Cheat Sheet

Getting Started:

Initialize a Git Repository:

  • To start tracking your project with Git, navigate to your project folder and run:
git init
Enter fullscreen mode Exit fullscreen mode

Configure Your Identity:

  • Set your name and email address for your Git commits:
git config - global user.name "Your Name"
git config - global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

Basic Workflow:

Check Repository Status:

  • See the current status of your repository:
git status
Enter fullscreen mode Exit fullscreen mode

State Changes:

  • Add changes to the staging area (preparing them for a commit):
git add filename # To stage a specific file
git add . # To stage all changes
Enter fullscreen mode Exit fullscreen mode

Commit Changes:

  • Create a new commit with a message:
git commit -m “Your commit message”
Enter fullscreen mode Exit fullscreen mode

View Commit History:

  • See a list of previous commits:
git log
Enter fullscreen mode Exit fullscreen mode

Branches:

Create a New Branch:
— Create a new branch for your work:

git branch [branch_name]
Enter fullscreen mode Exit fullscreen mode

Switch to a Branch:

  • Change to a different branch:
git checkout [branch_name]
Enter fullscreen mode Exit fullscreen mode

Create and Switch to a New Branch:

  • Combine branch creation and checkout in one step:
git checkout -b [branch_name]
Enter fullscreen mode Exit fullscreen mode

List Branches:

  • View a list of all branches in your repository:
git branch
Enter fullscreen mode Exit fullscreen mode

Delete a Branch (Locally):

  • Remove a branch that is no longer needed:
git branch -d [branch_name]
Enter fullscreen mode Exit fullscreen mode

Merging:

Merge Changes from Another Branch:

  • Combine changes from one branch into another:
git merge [branch_name]
Enter fullscreen mode Exit fullscreen mode

Remote Repositories:

Clone a Remote Repository:

  • Start working with an existing repository hosted on a remote server:
git clone [repository_url]
Enter fullscreen mode Exit fullscreen mode

Add a Remote Repository:

  • Connect your local repository to a remote server:
git remote add [remote_name] [repository_url]
Enter fullscreen mode Exit fullscreen mode

Pull Changes from a Remote Repository:

  • Update your local repository with changes from the remote:
git pull [remote_name] [branch_name]
Enter fullscreen mode Exit fullscreen mode

Push Changes to a Remote Repository:

  • Share your local changes with the remote repository:
git push [remote_name] [branch_name]
Enter fullscreen mode Exit fullscreen mode

Remember that Git offers many more features and commands, but these basics should help you get started with version control. As you become more comfortable with Git, you can explore more advanced concepts and commands.

Top comments (0)