Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Git Tips and Tricks

Introduction

Git is a powerful version control system that allows developers to manage their code and collaborate with others effectively. In this lesson, we will explore some essential tips and tricks that can enhance your Git experience.

Basic Commands

Understanding basic Git commands is crucial for effective version control.

Tip: Always write clear commit messages to make your project history understandable.

Common Commands

  • git init - Initializes a new Git repository.
  • git clone [url] - Clones an existing repository.
  • git add [file] - Stages changes for commit.
  • git commit -m "message" - Commits staged changes with a message.
  • git push - Pushes local changes to a remote repository.
  • git pull - Fetches and merges changes from the remote repository.

Branching Strategies

Branching is a powerful feature in Git that allows you to work on different versions of your code simultaneously.

Creating and Merging Branches

git branch new-feature
git checkout new-feature
# Make changes
git add .
git commit -m "Add new feature"
git checkout main
git merge new-feature

To delete a branch after merging:

git branch -d new-feature

Using Stash

Use git stash to save your uncommitted changes temporarily.

git stash
# Switch branches or pull changes
git stash apply
Note: You can use git stash list to view all stashed changes.

Rebasing vs Merging

Rebasing is a way to integrate changes from one branch into another. It can create a cleaner project history.

Rebase Command

git checkout feature-branch
git rebase main

Warning: Avoid rebasing shared branches as it can lead to unnecessary complications.

FAQ

What is the difference between Git and GitHub?

Git is a version control system, while GitHub is a platform that hosts Git repositories online.

What does "forking" a repository mean?

Forking a repository creates a personal copy of someone else's project which you can modify without affecting the original.

How do I undo a commit?

You can undo the last commit by using git reset HEAD~1.