Back to Git & Dev Tools
2026-04-126 min read

Git in Zsh

Learn Git in Zsh step by step with clear examples and exercises.

Title: Git in Zsh: A full guide for Developers

Why This Matters

In the realm of software development, version control systems are indispensable tools that help manage and track changes to codebases. One such system is Git, which has become a standard for developers worldwide due to its efficiency and flexibility. However, using Git effectively requires appropriate configuration, and one such configuration is setting it up with Zsh (Z shell). This lesson will guide you through the process of installing and configuring Git in your Zsh environment, providing practical examples, common mistakes, and answers to frequently asked questions.

Prerequisites

Before diving into Git in Zsh, ensure you have a basic understanding of:

  1. Command Line Interface (CLI): Familiarity with navigating directories, running commands, and managing files using the command line is essential.
  2. Zsh: Awareness of the Z shell, its features, and how to use it as your default shell.
  3. Git Basics: Understanding the fundamental concepts of Git such as repositories, commits, branches, and merges will help you grasp the material more easily. Additionally, knowledge of common Git commands like git add, git commit, git pull, and git push is beneficial. Familiarity with Zsh plugins, such as Oh-my-zsh, can also be helpful but is not strictly necessary for this lesson.

Core Concept

Installing Git in Zsh

To install Git on a Unix-based system like Linux or macOS, follow these steps:

  1. Update package lists: Run sudo apt-get update (for Debian-based systems) or brew update (for macOS with Homebrew).
  2. Install Git: Install Git using the following command:
sudo apt-get install git # for Debian-based systems
brew install git # for macOS with Homebrew
  1. Verify installation: To check if Git has been installed correctly, run git --version. You should see the version of Git displayed on your terminal.

Configuring Git in Zsh

After installing Git, you'll want to configure it for your user account and set up some useful options:

  1. Global configuration: Initialize Git with your global settings by running git config --global user.name "Your Name" and git config --global user.email "your-email@example.com". Replace "Your Name" and "your-email@example.com" with your actual name and email address.
  2. Colorized output: To make Git outputs more readable, add the following line to your ~/.zshrc file:
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_FORMAT="%s %b (%a) %n %r"
  1. Alias: Create a convenient alias for the git status command by adding the following line to your ~/.zshrc file:
alias gst='git status -s'
  1. Auto-completion: To enable Git auto-completion, add the following lines to your ~/.zshrc file:
source /usr/share/git/completion/git-completion.bash
source /usr/share/git/completion/git-completion.zsh
  1. Save and reload: Save the ~/.zshrc file and reload it using source ~/.zshrc.

Git in Action with Zsh

Now that Git is set up in your Zsh environment, let's create a new repository and make some changes:

  1. Create a new directory: Navigate to the desired location using the command line and create a new directory called my-repo:
mkdir my-repo
cd my-repo
  1. Initialize Git: Initialize the new repository with Git by running git init.
  2. Create a file: Create a new file named README.md using your favorite text editor.
  3. Stage and commit changes: Add the new file to the staging area using git add ., then commit it with a message: git commit -m "Initial commit".
  4. View commit history: Check the commit history with git log.

Worked Example

In this section, we'll walk through creating a new repository, making changes, and resolving conflicts using Git in Zsh.

  1. Create a new directory called example-repo:
mkdir example-repo
cd example-repo
git init
  1. Create two files named file1.txt and file2.txt.
  2. Make changes to both files, for example:
  • Add content to file1.txt: echo "Hello World" > file1.txt
  • Modify the content of file2.txt: echo "Goodbye World" > file2.txt
  1. Stage and commit changes for both files:
git add .
git commit -m "Initial commit"
  1. Create a new branch called feature-branch:
git checkout -b feature-branch
  1. Make more changes to the files in the feature-branch. For example, modify the content of file1.txt and add a new file named file3.txt.
  2. Stage and commit changes for the branch:
git add .
git commit -m "Add more content"
  1. Switch back to the master branch:
git checkout master
  1. Merge the changes from feature-branch into master. This will result in a merge conflict:
git merge feature-branch
  1. Resolve the conflicts manually by editing the conflicting files and committing the resolved version:
  • Edit the conflicting files (e.g., file1.txt) using your text editor.
  • Save the changes and stage them: git add file1.txt
  • Commit the resolved version with a message: git commit -m "Resolved merge conflict"
  1. Delete the feature-branch if no longer needed:
git branch -d feature-branch

Common Mistakes

1. Forgetting to add files before committing

When you make changes to your files, don't forget to stage them using git add or git add . (to stage all changes) before committing with git commit -m "".

2. Ignoring merge conflicts

When merging branches, Git may encounter conflicts that require manual resolution. If you ignore these conflicts, your codebase may become inconsistent or corrupted. Always resolve the conflicts manually and commit the resolved version.

3. Committing unfinished work

Avoid committing half-finished work or incomplete changes to your repository. This can lead to confusion, errors, and unnecessary revisions in your commit history. Make sure to stage and commit only well-tested, functional changes.

Practice Questions

  1. How do you initialize a new Git repository in Zsh?
  • Run git init in the desired directory.
  1. What is the purpose of the GIT_PS1_SHOWDIRTYSTATE=1 and GIT_PS1_FORMAT lines in your ~/.zshrc file?
  • These lines configure Git to display the status of dirty files (i.e., modified or unstaged changes) in your command prompt.
  1. How do you create a new branch in Git and switch to it?
  • Run git checkout -b .
  1. What should you do when encountering merge conflicts while merging branches in Git?
  • Resolve the conflicts manually by editing the conflicting files, saving the changes, staging them with git add, and committing the resolved version.
  1. What is the purpose of the git status -s alias created in your ~/.zshrc file?
  • The alias provides a compact, easy-to-read summary of the current Git status (i.e., staged and unstaged changes) in your repository.

FAQ

Q: What is the difference between Bash and Zsh?

A: Bash is the default shell for most Unix-based systems, while Zsh is an enhanced version of Bash that offers additional features such as autocompletion, plugins, and customizable options.

Q: How do I set Git to use my preferred text editor when committing changes?

A: You can configure your preferred text editor by setting the core.editor configuration variable in Git. For example, if you want to use Vim as your text editor, add the following line to your ~/.zshrc file: git config --global core.editor vim.

Q: How do I undo a commit in Git?

A: To undo the latest commit, use the git reset --soft HEAD^. This will remove the most recent commit from your history without deleting any files.

Q: What is the purpose of the .gitignore file in a Git repository?

A: The .gitignore file is used to specify which files and directories Git should ignore when committing changes, helping to keep your repository clean and organized.

Git in Zsh | Git & Dev Tools | XQA Learn