A definitive guide for software development
A definitive guide for software development

Version Control With Git: Learn Git In 5 Minutes

Git Cheat Sheet

Imagine this: you’re working on a complex software project. You’ve written tons of code, but halfway through, you realize a critical bug sneaked in a few versions back. Panic sets in – how do you revert to a stable version without losing all your recent progress? Enter the hero of collaborative development: Git. Git is a distributed version control system (VCS) that allows you to track changes in your code over time. It’s like a magic time machine for your project, letting you rewind, compare versions, and collaborate seamlessly with other developers.

This blog post equips you with the essential Git commands to master collaborative development. We’ll explore core concepts, practical workflows, and even tackle some common Git challenges.

Why Version Control Matters?

Before diving into Git, let’s understand why version control is crucial:

  • Track Changes: See exactly what changed in your codebase between versions.
  • Rollback to Stable Versions: Accidentally introduced a bug? Quickly revert to a working version.
  • Collaboration: Work with other developers on the same project without conflicts.
  • Version History: Keep track of your project’s evolution and understand when features were added/modified.
  • Branching & Merging: Experiment with new features without impacting your main codebase.

Getting Started with Git

Let’s break down the basic steps for using Git:

1. Installation:

Download and install Git from https://git-scm.com/downloads.

2. Initialize a Git Repository:

Open your terminal and navigate to your project directory. Run the following command to initialize a new Git repository:

git init

This creates a hidden folder named .git in your project directory, which stores all the Git magic.

3. Staging Changes:

Not all changes in your working directory (your project files) are tracked by Git yet. Use the git add command to tell Git which files you want to track:

git add filename.py

You can add multiple files or use git add . to add all modified files.

4. Committing Changes:

Once you’ve staged changes, create a snapshot of your project with a descriptive commit message using git commit:

git commit -m "Added a new login feature"

A commit message helps you understand what changed in this specific version.

5. Viewing the Version History (Log):

Use git log to view the history of your commits, including the messages:

git log

This displays a list of commits with their commit IDs, author, date, and message.

6. Downloading a Remote Repository:

So far, we’ve been working with a local Git repository. To collaborate, you need to connect to a remote repository like GitHub (https://github.com/). Follow their instructions on cloning (downloading) a repository to your local machine. This creates a local copy of the remote repository with its version history.

7. Pushing Changes (Collaboration):

Once you’ve made some commits, you can push your local changes to the remote repository using git push:

git push origin main

Here, “origin” is a common name for the remote repository and “main” is the branch you’re pushing changes to (we’ll discuss branches later).

8. Pulling Updates (Collaboration):

Similarly, to pull the latest changes from the remote repository and merge them into your local copy, use git pull:

git pull origin main

This ensures you’re always working on the most recent version of the project.

Branching & Merging for Collaborative Development

Now that you have the basics down, let’s explore Git’s powerhouses: branches and merging.

Branching:

A branch is a separate line of development in your project. Imagine a branching tree – the main branch represents the main development line, and you can create branches to experiment with new features or bug fixes without affecting the main line.

To create a new branch, use git branch:

git branch feature/login  # Creates a new branch named "feature/login"

Now, your work is isolated in this branch. You can commit changes to the feature branch without affecting the main branch.

Merging:

Once you’re happy with the changes in your feature branch, you need to integrate them back into the main line. This is achieved through merging.

To merge the “feature/login” branch into the “main” branch:

git checkout main        # Switch to the "main" branch
git merge feature/login   # Merge the "feature/login" branch into "main"

Git intelligently merges changes, but sometimes conflicts arise when different developers modify the same lines of code in separate branches. Here’s how to handle merge conflicts:

Resolving Merge Conflicts:

  1. Identifying Conflicts: Git will signal a merge conflict by marking the conflicted files with special markers (usually <<<<<<<, =======, >>>>>>>). Open these files in your text editor to see the conflicting changes.
  2. Manually Editing Files: You’ll need to manually edit the files to decide which changes to keep and which to discard. Git provides markers indicating the original content (<<<<<<<), your changes (=======), and the other developer’s changes (>>>>>>>).
  3. Resolving Conflicts and Committing: Once you’ve resolved the conflicts by editing the files, remove the Git markers and save the files. Finally, run git add on the resolved files and git commit to create a new commit that merges the changes from both branches.

Here’s an example of a conflict marker:

<<<<<<< HEAD
This line was modified in your branch.
=======
This line was modified in the other branch.
>>>>>>> feature/login

In this case, you need to decide which version (or a combination) to keep and remove the conflict markers accordingly.

Additional Resources:

Advanced Git Features:

Beyond the basics, Git offers various features to enhance your workflow:

  • Stashing: Temporarily save uncommitted changes for later use.
  • Branching Strategies: Utilize branching strategies like Gitflow to manage complex development projects.
  • Remotes & Collaboration: Collaborate with others by pushing and pulling changes from remote repositories like GitHub.
  • Ignoring Files: Use .gitignore files to specify files or folders to exclude from version control.

By mastering these core concepts and exploring advanced features, Git empowers you to manage your code effectively and collaborate seamlessly with other developers.

Git Cheat Sheet

This is a quick reference for common Git commands. Remember, consulting the official Git documentation for detailed explanations is always recommended!

General:

  • git init: Initializes a new Git repository in the current directory.
  • git clone [URL]: Creates a local copy of a remote repository.
  • git config: Sets Git configuration options (e.g., username, email).
  • git status: Shows the current status of your working directory and staging area.

Staging:

  • git add [file]: Adds a file to the staging area for the next commit.
  • git add .: Adds all modified and tracked files to the staging area.
  • git rm [file]: Removes a file from the working directory and staging area (use with caution!).

Committing:

  • git commit -m "[message]": Creates a new commit with a descriptive message.

Viewing History:

  • git log: Shows the commit history of the current branch.
  • git log --oneline: Shows a one-line summary of the commit history.
  • git log [commit-ish]: Shows the commit history starting from a specific commit.

Branching:

  • git branch: Lists all local branches.
  • git branch [branch-name]: Creates a new branch.
  • git checkout [branch-name]: Switches to a different branch.
  • git merge [branch-name]: Merges another branch into the current branch.
  • git delete [branch-name]: Deletes a local branch (after merging it if necessary).

Remote Repositories:

  • git remote: Lists all configured remote repositories.
  • git remote add [name] [url]: Adds a new remote repository.
  • git fetch [remote]: Downloads the latest changes from a remote repository without merging.
  • git pull [remote] [branch-name]: Fetches and merges changes from a remote branch.
  • git push [remote] [branch-name]: Pushes local commits to a remote branch.

Undoing Changes:

  • git reset HEAD [file]: Unstages a file from the staging area. (Carefully consider using git checkout HEAD [file] instead to avoid losing work).
  • git checkout .: Discards all unstaged changes in the working directory.
  • git revert [commit-ish]: Creates a new commit that reverses the changes introduced in a specific commit.

Stashing:

  • git stash: Temporarily saves your uncommitted changes for later use.
  • git stash list: Lists all stashed changes.
  • git stash pop: Applies the most recent stash and removes it from the list.

Additional Tips:

  • Use git diff to see the difference between files (uncommitted vs. staged, staged vs. committed, etc.).
  • Utilize git tag to create bookmarks for specific versions of your project.
  • Explore git ignore to create a file that specifies patterns to exclude from version control.

Share this article
Shareable URL

Read next

Subscribe to The Software Development Blog
Get updates on latest posts and exclusive deals straight to your inbox