20 Git Commands
Git Basics
Before we dive into the more advanced Git commands, let's review some basics.
Initializing a Git Repository
To initialize a Git repository, navigate to the directory of your project in the command line and enter the following command:
git init
This will create a new Git repository in your current directory.
Cloning a Repository
To clone an existing Git repository, enter the following command:
git clone [repository URL]
This will create a copy of the repository in your local directory.
Checking the Status of a Repository
s To check the status of your repository, enter the following command:
git status
This will show you any changes that have been made to your code since the last commit.
Staging Changes
To stage changes for a commit, use the following command:
git add [file name]
This will add the specified file to the staging area, ready for commit.
Committing Changes
To commit changes, use the following command:
git commit -m "commit message"
This will create a new commit with the specified commit message.
Branching and Merging
Branching and merging are essential Git commands for managing multiple versions of your codebase.
Creating a Branch
To create a new branch, use the following command:
git branch [branch name]
This will create a new branch with the specified name.
Switching Between Branches
To switch between branches, use the following command:
git checkout [branch name]
This will switch you to the specified branch.
Merging Branches
To merge a branch into your current branch, use the following command
git merge [branch name]
This will merge the specified branch into your current branch.
Resolving Merge Conflicts
If there are conflicts when merging branches, use the following command to resolve them:
git mergetool
This will open a tool to help you resolve the conflicts.
Collaborating with Others
Git makes it easy to collaborate with others on a coding project.
Adding a Remote
To add a remote repository, use the following command:
git remote add [name] [repository URL]
This will add a remote repository with the specified name and URL.
Fetching Changes
To fetch changes from a remote repository, use the following command:
git fetch [remote name]
This will fetch any changes made to the remote repository.
Pulling Changes
To pull changes from a remote repository, use the following command:
git pull [remote name] [branch name]
This will pull any changes made to the specified branch of the remote repository.
Pushing Changes
To push changes to a remote repository, use the following command:
git push [remote name] [branch name]
This will push any changes made to the specified branch of the remote repository.
Advanced Git Commands
Now that we’ve covered the basics of Git, let’s take a look at some more advanced Git commands.
Checking Out Specific Commits
To check out a specific commit, use the following command:
git checkout [commit hash]
This will check out the specified commit, allowing you to view the code at that point in time.
Amending Commits
To amend a commit, use the following command:
git commit --amend
This will allow you to add any changes to the previous commit.
Reverting Commits
To revert a commit, use the following command:
git revert [commit hash]
This will create a new commit that undoes the changes made in the specified commit.
Rebasing Branches
To rebase a branch, use the following command
git rebase [base branch] [topic branch]
This will apply the changes made in the topic branch onto the base branch.
Cherry-Picking Commits
To cherry-pick a commit, use the following command:
git cherry-pick [commit hash]
This will apply the changes made in the specified commit onto your current branch.
Stashing Changes
To stash changes that you don’t want to commit yet, use the following command:
git stash save [message]
This will save your changes to a stash, allowing you to retrieve them later.
Conclusion
In this article, we’ve covered 20 Git commands that will make you a version control pro. By mastering these commands, you’ll be able to more effectively manage your codebase, collaborate with others, and track changes to your code. Whether you’re a beginner or an experienced Git user, these commands will help take your version control skills to the next level.