Skip to main content

Appendix D - Git Version Control

Introduction

Before diving into the specifics of Git, it's important for beginners to understand what version control is and why it's essential for software development, especially in collaborative environments. Git is a distributed version control system that helps developers track and manage changes to their codebase. It allows multiple developers to work on the same project without conflicting, and it makes it easy to revert back to previous versions if something goes wrong.

For those new to Git, here are some foundational concepts and resources:

  • What is Version Control?

    • Version control systems allow you to keep track of your software at various stages of development, ensuring that changes are recorded and can be reverted if necessary.
  • Why Git?

    • Git is widely used because of its efficiency in handling projects of all sizes and its support for non-linear development.
  • Free Resources for Learning Git:

After gaining a basic understanding of Git and version control, you can proceed to the more detailed aspects of this guide.


Initial Setup and Configuration

  • Install Git: Download and install Git from the official site git-scm.com.

  • Configure Git:

    • Define your identity:
      • Name: git config --global user.name "Your Name"
      • Email: git config --global user.email "your_email@example.com"
    • Set default editor: git config --global core.editor "editor_name"
    • Check configuration: git config --list

Creating a New Repository

  • Initialize a new repository:
    • In your project directory, run git init to start version control.

Basic Git Workflow for C# Projects

  • Checking status: Use git status to view the state of your work.

  • Staging files:

    • For a single file: git add <filename>
    • For all modified files: git add .
  • Committing changes: git commit -m "Descriptive message"


Branching, Merging, and Managing Conflicts

  • Branch management:

    • Create: git branch <branch_name>
    • Switch: git checkout <branch_name>
    • List all branches: git branch
  • Merging branches:

    • Prepare by switching to the target branch: git checkout <target_branch>
    • Merge the source branch: git merge <source_branch>
    • Resolve conflicts if necessary.

Collaboration and Remote Repositories

  • Managing remote repositories:

    • Add a remote: git remote add origin <repository_url>
    • View remotes: git remote -v
  • Pushing and pulling changes:

    • Initial push: git push -u origin <branch_name>
    • Standard push: git push
    • Pulling updates: git pull origin <branch_name>

Exploring Commit History

  • Reviewing logs:
    • Basic log: git log
    • Graphical log: git log --graph --oneline --decorate --all

Amending and Reversing Changes

  • Undoing local modifications:

    • Specific file: git checkout -- <filename>
    • All unstaged changes: git checkout -- .
  • Reverting commits: git revert <commit_hash>

  • Resetting to a specific state:

    • Soft reset (preserves changes): git reset --soft <commit_hash>
    • Hard reset (discards changes): git reset --hard <commit_hash>

Advanced Git Techniques

  • Stashing work: git stash / git stash apply
  • Rebasing interactively: git rebase -i <base_commit_hash>
  • Cherry-picking commits: git cherry-pick <commit_hash>

C#-Specific Git Practices

  • .gitignore for C# projects:
    • Typical entries: bin/, obj/, *.user, *.suo, *.cache, *.log, /packages/, *.csproj.user, *.dll, *.pdb, *.exe

Git Best Practices

  • Commit frequently with clear, descriptive messages.
  • Utilize branches for developing new features, bug fixes, and experiments.
  • Regularly sync your work with remote repositories to safeguard progress.

Further Learning and Support

  • Official Git documentation: git-scm.com/doc
  • Engage with online tutorials and courses to deepen understanding.
  • Participate in community forums and Q&A sites like Stack Overflow for support.