DEV Community

Cover image for 7 BASIC GIT COMMANDS
Abigael Wairimu
Abigael Wairimu

Posted on

7 BASIC GIT COMMANDS

In this article, we'll learn 7 commonly used Git commands that form the backbone of version control workflows. They will empower you as a developer to manage projects effectively and collaborate seamlessly.
First, we must initialize a .git repository on our project.

git init
To initialize a new Git repository in your current directory, you can use the git init command in the terminal.
This command creates a new .git directory in your current directory to track and manage your project's version history.

Example:

An image demonstrating how git init works

  1. Switch to our working directory.
  2. Create a .git directory in our project.

git add[file]
To add all the changes in your current directory to the staging area in Git, you can use the git add .
This command stages all changes in the current directory and its subdirectories.

Example:

An image demonstrating how git add works

  1. Stage all changes made to our directory
  2. To add specific changes from the file index.html to the staging area in Git, you can use the git add index.html

git commit -m "Your commit message"
This command creates a new commit with your changes and a message describing what the commit does.

Example:

An image demonstrating how git commit works

  1. Commit the changes we staged.
  2. Your commit message should be brief to help you and others understand what was changed and why.

git pull
To update your local repository with the latest changes from the remote repository, you can use the git pull command in the terminal.
This command fetches the changes from the remote repository and merges them into your current branch.

Example:
An image demonstrating how git pull works

  1. After running this command, your local repository should be up-to-date with the remote repository.

git push
To upload your local repository changes to the remote repository, you can use the git push command in the terminal.
This command pushes the changes from your local repository to the remote repository.

Example:

An image demonstrating how git push works

  1. Update remote.
  2. To push your changes to the main branch of the remote repository named origin, you can use the git push -u origin main command in the terminal.

git status
To check the status of your local repository, you use the git status command in the terminal.
This command shows which files have changes that are staged for the next commit and which files have changes that are not staged yet.

Example:

An image demonstrating how git status works

  1. Check changes made to our repository.
  2. After running this command, you should see a list of files that have been modified, added, or deleted, along with their current status (staged, not staged, etc.).

git clone[repository_url]
To clone a remote repository to your local machine, you can use the git clone command followed by the URL of the repository.
This command creates a new directory with the same name as the repository, initializes a .git directory inside it, pulls down all the data for that repository, and checks out a working copy of the latest version.

Example:

An image demonstrating how git clone works

Thank you for taking the time to read this. I'll see you in the next one!
Happy coding!

Top comments (0)