Chelistico

It's a tech blog!

Mastering the Basics: Essential Git Commands

By Marcelo Vieyra, November 1st 2023 | 4 mins, 615 words

Git is a powerful and widely used version control system that plays a crucial role in modern software development. Whether you're a seasoned developer or just starting with Git, mastering the essential commands is key to efficiently managing your codebase. In this article, we'll dive into the fundamental Git commands that every developer should know.

1. Initializing a Git Repository


The first step in using Git is to initialize a repository. Navigate to your project's root directory and use the following command:

git init

This command creates a hidden .git directory in your project folder, where Git stores all its configuration and version history.

2. Cloning a Repository


If you're working on an existing project hosted on a Git repository, you can clone it to your local machine using:

git clone <repository_url>

Replace <repository_url> with the URL of the remote repository. This command will create a local copy of the project on your machine.

3. Staging Changes


Before committing changes, you need to stage them. You can use git add to stage specific files or directories:

git add <file_or_directory>

To stage all changes, use:

git add .

4. Committing Changes

Once your changes are staged, you can create a commit with a descriptive message using:

git commit -m "Your commit message"

Commits serve as snapshots of your project at specific points in time, and the message should describe the purpose of the changes.

5. Checking the Status


To see the current status of your repository, including the files that are modified, staged, or untracked, use:

git status

This command provides valuable information about the state of your project.

6. Viewing Commit History


You can view the commit history, including commit messages and unique hashes, with:

git log

This is particularly useful for tracking changes and understanding the project's development history.

7. Branching


Branching is a powerful feature in Git that allows you to work on new features or bug fixes without affecting the main codebase. To create a new branch, use:

git branch <branch_name>

Switch to the new branch with:

git checkout <branch_name>

Or combine both actions using:

git checkout -b <branch_name>

8. Merging Branches


Once you've completed work on a branch, you can merge it back into the main branch (often called master or main) with:

git merge <branch_name>

This integrates your changes into the main branch.

9. Pulling and Pushing Changes


When working with remote repositories, you'll frequently need to update your local copy and share your changes with others. Use git pull to update your local copy with changes from the remote repository:

git pull

To send your local changes to the remote repository, use:

git push

10. Ignoring Files


You can create a .gitignore file to specify which files and directories should be ignored by Git. This is useful for excluding build artifacts, log files, and other non-essential files from version control.

11. Discarding Changes


If you want to discard changes made to a file and revert it to the last committed version, use:

git checkout -- <file>

This can be helpful when you've made unwanted changes.

12. Reverting Commits


To undo the effects of a specific commit and create a new commit that undoes the changes, use:

git revert <commit_hash>

Replace <commit_hash> with the hash of the commit you want to revert.

These are the foundational Git commands that every developer should be familiar with. As you become more comfortable with Git, you can explore advanced features and workflows. Git is a versatile tool that can greatly enhance your ability to collaborate on software projects and manage your code effectively.

Remember, practice makes perfect. Experiment with these commands in a safe environment to build your Git skills. Happy coding!