Git Commands: A Beginner’s Guide to Git Commands

Like Add, Commit, Push, Pull, Merge, and More

Ink Insight 🧘🏼
Geek Culture
6 min readMar 10, 2023

--

Photo by Praveen Thirumurugan on Unsplash

Hello everyone, today we’ll be discussing Git. As of today’s date, it’s Wednesday, 2023 (fun fact: a new TV show was recently released this year that I just remembered when I wrote ‘Wednesday’. 🙂 Let’s get started!

tl;dr

  • git init: Use this command to initialize a new Git repository in a directory where you want to track changes to your code.
  • git status: Use this command to check the status of your repository and see which files have been modified or staged for commit.
  • git add: Use this command to stage changes to your code before committing them. You can add individual files or entire directories using this command.
  • git commit: Use this command to save your changes to the repository with a descriptive commit message. You should commit your changes regularly, ideally, after each logical change you make to your code.
  • git log: Use this command to view a history of commits in your repository. This can be useful for tracking changes or debugging issues.
  • git branch: Use this command to create, switch between, or delete branches in your repository. You might use branches to work on different features or bug fixes in parallel or to maintain separate development and production branches.
  • git checkout: Use this command to switch between different branches in your repository or to revert your working directory to a previous commit. You might use this command to test changes or to work on different features separately.
  • git merge: Use this command to merge changes from one branch into another. You might use this command to integrate changes from a feature branch into your main development branch.
  • git pull: Use this command to fetch changes from a remote repository and merge them into your current branch. You might use this command to update your local repository with changes made by other contributors.
  • git push: Use this command to send your changes to a remote repository and update it with your changes. You might use this command to share your code with other contributors or to deploy it to a production environment.
  • git clone: Use this command to download a copy of a remote repository to your local machine. You might use this command to contribute to an existing project or to create a local backup of a remote repository.

In this blog, we’ll go over the most common Git commands that beginners need to know and explain how to use them.

Photo by Mohammad Rahmani on Unsplash

it’s Basics

Before we dive into specific Git commands, let’s go over some basic Git concepts.

Repository: A Git repository is a directory that contains your code and all the Git data that tracks changes to it.

Commit: A commit is a snapshot of your code at a specific point in time. Each commit has a unique identifier (a SHA-1 hash) that Git uses to keep track of it.

Branch: A branch is a separate line of development that allows you to work on multiple features or versions of your code at the same time.

Remote: A remote is a Git repository that’s hosted on a different server than your local repository. You can push your changes to a remote repository to share your code with others, or pull changes from a remote repository to update your local code.

Git Commands

Now that we have an understanding of basic Git concepts, let’s dive into the most commonly used Git commands.

2.1 Git Init

The first step in using Git is to initialize a new Git repository. To do this, navigate to your project’s directory and run the following command:

git init

This command creates a new Git repository in your current directory and initializes it with default settings.

2.2 Git Add

Once you’ve initialized a new Git repository, you need to tell Git which files to track. To do this, use the git add command:

git add <filename>

This command stages the specified file for the next commit. You can also use the git add command with a wildcard to add multiple files at once:

git add .

This command stages all files in your current directory for the next commit.

2.3 Git Commit

After you’ve staged your changes using git add, you need to create a new commit to save those changes to your Git repository. To do this, use the git commit command:

git commit -m "follow inkinsight"

This command creates a new commit with the changes you’ve staged using git add. The -m flag allows you to specify a commit message that describes the changes you’ve made.

Photo by Yancy Min on Unsplash

2.4 Git Status

To see the current status of your Git repository, use the git status command:

git status

This command shows you which files have been modified, staged, or committed since your last commit.

2.5 Git Log

To view a history of all the commits in your Git repository, use the git log command:

git log

This command shows you a list of all the commits in your repository, including the commit message, author, and date.

2.6 Git Branch

To create a new branch in your Git repository, use the git branch command:

git branch <branch-name>

This command creates a new branch with the specified name. You can then switch to this branch using the git checkout command.

2.7 Git Checkout

To switch to a different branch in your Git repository, use the git checkout command:

git checkout <branch-name>

This command switches your working directory to the specified branch. If the branch doesn’t exist yet, you’ll need to create it using the git branch command first.

Photo by Marc Rafanell López on Unsplash

2.8 Git Merge

To merge changes from one branch into another, use the git merge command:

git merge <branch-name>

This command combines the changes from the specified branch into your current branch. If there are conflicts between the two branches, Git will prompt you to resolve them before the merge can be completed.

2.9 Git Pull

To update your local repository with changes from a remote repository, use the git pull command:

git pull <remote> <branch-name>

This command fetches the latest changes from the specified remote repository and merges them into your current branch.

2.10 Git Push

To push your changes to a remote repository, use the git push command:

git push <remote> <branch-name>

This command sends your commits to the specified remote repository and updates it with your changes. If you’re pushing changes to a remote repository for the first time, you’ll need to use the -u flag to set the upstream branch:

git push -u <remote> <branch-name>

This command sets the upstream branch to the specified remote repository and branch, so you can use git pull and git push without specifying the remote and branch every time.

2.11 Git Clone

To download a copy of a remote repository to your local machine, use the git clone command:

git clone <repository-url>

This command creates a new directory on your local machine and downloads a copy of the remote repository into it.

Conclusion

Today, we’ve covered the most commonly used Git commands that beginners need to know. By using these commands, you can track changes to your code, collaborate with others, and easily revert to previous versions when needed. Remember to always use descriptive commit messages and to regularly push your changes to a remote repository to keep your code safe and share it with others. With practice and experience, you’ll become a Git expert in no time.

Don’t miss a beat — subscribe to my Medium newsletter to get my latest articles and content before anyone else. And if you enjoy what you’re reading, be sure to follow me for even more great content!

And, as always, thank you for taking the time to read and engage with my content — your support means the world to me!

--

--

Ink Insight 🧘🏼
Geek Culture

Discover the intersection of DevOps, InfoSec, and mindfulness with Ink Insight. Follow for valuable insights! ✍︎ 👨‍💻 🧘🏼