Mastering Git: A Beginner’s Guide to Essential Commands and Collaboration

Git is a helpful tool used by programmers to keep track of changes made to their projects. Imagine it as a safe and secure storage system for different versions of your project files. This guide will show you some essential Git commands to get you started!

Let’s get Git installed on your laptop!

  1. Download Git: Go to the Git website and download the version for your operating system (Windows, macOS, or Linux).

  2. Install Git: Run the installer and accept the default settings.

  3. You’re all set! Git is now installed on your computer.

Understanding the Git Workflow

Git Stages

This diagram represents the basic workflow in Git:

  1. Workspace: Your local files where you make changes.

  2. Staging Area is where you prepare your changes.

  3. Local Repository is where Git stores the complete history.

  4. Remote Repository: where we upload changes to a remote server.

Setting Up Git

1. Configure Git

Before using Git, set your name and email address:

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

Now, you can verify these configurations by running the below commands.

git config --global user.name
git config --global user.email

Your git is now configured.

Working with Repositories

2. Clone a Repository

To get a copy of an existing repository:

$ git clone <repository-url>

3. Initialize a New Repository

Start tracking your project with Git:

$ git init

Example:

$ git init my-new-project

Making Changes

4. Check Status

See the status of your working directory and staging area:

$ git status

5. Add Changes

Stage changes for commit:

$ git add <file>

Example:

$ git add index.html

To add all changes:

$ git add .

6. Commit Changes

Save changes to the repository:

$ git commit -m "Your commit message"

HEAD points to the latest commit in your active branch.

7. Reset Changes

Unstage changes or reset to a previous state:

$ git reset <file>

To reset the entire working directory:

$ git reset --hard

Branching and Merging

8. Create a Branch

Branches allow you to work on different parts simultaneously. To create a new branch to work on:

$ git branch <branch-name>

9. Switch Branches

Move between branches:

$ git checkout <branch-name>

10. View Branches

List all branches in the repository:

$ git branch

Synchronizing with Remote Repositories

11. Push Changes

Upload local changes to a remote repository:

$ git push origin <branch-name>

12. Pull Changes

Fetch and merge changes from a remote repository:

$ git pull origin <branch-name>

13. Fetch Changes

Download updates from a remote repository without merging:

$ git fetch

Reviewing History

14. View Log

See a history of commits:

$ git log

Example:

$ git log --oneline

Advanced Commands

15. Rebase Changes

Reapply commits on top of another base tip:

$ git rebase <branch-name>

Simple Git for Collaboration

Git facilitates collaboration among multiple developers by allowing them to work on different parts of the project simultaneously. Key concepts include:

  • Forking: This means making a copy of someone else’s project (repository) so you can work on it without affecting the original project. For example, if you see a project on GitHub that you like, you can “fork” it to create your version where you can make changes.

  • Pull Requests: After you make changes in your forked project, you can suggest those changes to the original project by creating a “pull request.” It’s like saying, “Hey, I’ve made some improvements — can you review and possibly add them to the main project?”

  • Merge Conflicts: These happen when two people make changes to the same part of a file at the same time, and Git doesn’t know which version to keep. To fix a merge conflict, you need to go into the file and decide which changes should stay, then save and commit those changes.

Ignoring Files

Use a .gitignore file to exclude specific files or folders from version control. Create a .gitignore file and list the files/folders to ignore:

# Example .gitignore file
node_modules/
*.log
.env

Example:

echo "node_modules/" >> .gitignore
echo "*.log" >> .gitignore
echo ".env" >> .gitignore

Remote Repositories in Detail

Remote repositories (like GitHub, GitLab, and Bitbucket) are online places where your code is stored, so others can see and work on it with you. To push and pull code from these platforms, use:

$ git push origin <branch-name>
$ git pull origin <branch-name>

Undoing Mistakes

To undo a commit:

$ git revert <commit-hash>

To roll back to a previous commit:

$ git checkout <commit-hash>

By mastering these basic commands, you can start working efficiently with Git and manage your code like a pro. Thanks for reading till the end! Happy coding!

Let’s stay connected! Find me on LinkedIn.

0
Subscribe to my newsletter

Read articles from Aayushi Chourasiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aayushi Chourasiya
Aayushi Chourasiya

Java Backend Developer | Passionate about creating applications. Skilled in Java, Spring Boot, Hibernate, and AWS.