Tech

Git for Absolute Beginners: The 12 Commands You Need

· 6 min read

Git is the backbone of modern software development, enabling developers to track changes, collaborate, and manage code efficiently. For beginners, Git can seem intimidating, but mastering a handful of commands will get you up and running. This guide covers the 12 most essential Git commands, explained in simple terms with practical examples. Whether you’re a student, builder, or founder, these commands will help you take control of your code.

Getting Started with Git

Before diving into commands, you need to install and configure Git. Download Git from git-scm.com and install it on your system. Once installed, open a terminal and configure your username and email using these commands:

git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"

These settings are essential because Git uses them to track who made changes. Now, you’re ready to start using Git.

Initializing a Repository

To start using Git in your project, you need to initialize a repository. Navigate to your project folder in the terminal and run:

git init

This command creates a hidden .git folder that tracks all changes in your project. If you’re cloning an existing repository from a platform like GitHub, use:

git clone <repository-url>

This downloads the repository and sets up Git tracking automatically.

Checking the Status

The git status command is your go-to tool for understanding what’s happening in your repository. It shows which files are tracked, untracked, modified, or staged for commit. Run:

git status

This helps you stay organized and ensures you don’t miss any changes. For example, if you’ve modified a file but haven’t staged it, git status will remind you to do so.

Staging Changes

Before committing changes, you need to stage them. Use the git add command to add files or directories to the staging area. For example:

git add filename.txt

You can also stage all changes at once with:

git add .

Staging allows you to selectively commit changes, making it easier to manage your project.

Committing Changes

Once changes are staged, you can commit them to the repository. A commit is a snapshot of your project at a specific point in time. Use:

git commit -m "Your commit message"

The commit message should be clear and concise, describing what the changes do. For example, “Add login functionality” is better than “Made changes.”

Viewing the Commit History

The git log command shows the history of commits in your repository. Run:

git log

This displays details like the commit hash, author, date, and message. To exit the log, press q. If the log is too long, use git log --oneline for a concise view.

Creating and Switching Branches

Branches allow you to work on new features or fixes without affecting the main codebase. To create a new branch, use:

git branch new-feature

To switch to the branch, run:

git checkout new-feature

Alternatively, you can create and switch in one command:

git checkout -b new-feature

Branches are essential for collaborative workflows.

Merging Branches

Once you’ve completed work on a branch, you can merge it back into the main branch. First, switch to the branch you want to merge into (e.g., main):

git checkout main

Then, merge the feature branch:

git merge new-feature

If there are conflicts, Git will prompt you to resolve them before completing the merge.

Pulling and Pushing Changes

To sync your local repository with a remote one, use git pull and git push. The git pull command fetches changes from the remote repository and merges them into your local branch:

git pull origin main

After making local commits, push them to the remote repository with:

git push origin main

This ensures your team stays updated.

Undoing Changes

Mistakes happen, and Git provides tools to undo them. To unstage a file, use:

git reset filename.txt

To revert changes in a file to the last commit, run:

git checkout -- filename.txt

For more complex undo operations, explore git revert and git reset.

Ignoring Files

Some files, like logs or build artifacts, shouldn’t be tracked by Git. Create a .gitignore file in your project root and list the files or patterns to ignore. For example:

# Ignore log files
*.log

# Ignore build directories
/build

Git will skip these files when tracking changes.

Working with Remote Repositories

To add a remote repository, use:

git remote add origin <repository-url>

To view existing remotes, run:

git remote -v

Remote repositories enable collaboration and backup of your code. If you’re working on a hardware project and need to share CAD files, consider using Creomatrix’s 3D printing service alongside Git for version control.

With these 12 commands, you’ll have a solid foundation for using Git effectively. Practice them regularly, and you’ll soon feel confident managing your projects with Git.

#git#version control#coding#beginners#software