Logo pequeno-gafanhoto

Pequeno Gafanhoto

Git Basic commands

Git is a powerful version control system that enables developers to track changes, collaborate, and maintain a history of their codebase. To harness the full potential of Git, it's essential to familiarize yourself with its basic commands. In this blog post, we will walk you through the fundamental Git commands and provide practical code examples to help you get started with version control.

Initializing a Git Repository:

Creating a new Git repository from scratch.

Initializing an existing project as a Git repository.

Example: Initializing a new Git repository for a web project.

# Navigate to the project directory
cd project-folder

# Initialize the Git repository
git init

# Add project files to the repository
git add .

# Commit the initial changes
git commit -m "Initial commit"

Making Changes:

Tracking changes to files in the repository.

Reviewing the status of files and modifications.

Example: Modifying a file and committing the changes.

# Check the status of the repository
git status

# Modify a file
# ...

# Stage the changes for commit
git add modified-file.txt

# Commit the changes with a descriptive message
git commit -m "Update the modified-file.txt"

Branching and Merging

Creating and switching between branches.

Merging changes from one branch to another.

Example: Creating a new branch and merging it with the main branch.

# Create a new branch
git branch new-feature

# Switch to the new branch
git checkout new-feature

# Make changes and commit them
# ...

# Switch back to the main branch
git checkout main

# Merge the changes from the new-feature branch
git merge new-feature

Managing Remote Repositories

Cloning a remote repository.

Pushing local changes to a remote repository.

Example: Cloning a repository, making changes, and pushing them to the remote.

# Clone a remote repository
git clone https://github.com/user/repo.git

# Make changes to local files
# ...

# Stage and commit the changes
git add .
git commit -m "Add new features"

# Push the changes to the remote repository
git push origin main

Updating and Synchronizing

Fetching and pulling changes from a remote repository.

Resolving conflicts during merges.

Example: Updating the local repository with the latest changes from the remote.

# Fetch the latest changes from the remote repository
git fetch origin

# Merge the fetched changes with the local branch
git merge origin/main

# Resolve any merge conflicts
# ...

# Commit the merged changes
git commit -m "Merge remote changes"

# Update the local repository with the latest changes
git pull origin main

Git provides a robust set of commands that empower developers to efficiently manage version control in their projects. In this post, we covered the essential Git commands for initializing a repository, making changes, branching, merging, managing remote repositories, and updating your local codebase. By mastering these basic commands, you'll be well-equipped to leverage Git's capabilities and collaborate effectively with other developers while maintaining a reliable and organized version history of your code.