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:
- Switch to our working directory.
- 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:
- Stage all changes made to our directory
- To add specific changes from the file
index.html
to the staging area in Git, you can use thegit 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:
- Commit the changes we staged.
- 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.
- 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:
- Update remote.
- 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:
- Check changes made to our repository.
- 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:
Thank you for taking the time to read this. I'll see you in the next one!
Happy coding!
Top comments (0)