Home Git Essentials - A Practical Guide to Version Control
Post
Cancel

Git Essentials - A Practical Guide to Version Control

Introduction

Git is a tool that helps you manage changes to your files over time. Imagine you’re writing a book, and as you write, you save different versions of the book on your computer. Each version has some changes from the previous one. Now imagine that you want to keep track of all these changes and be able to switch back and forth between different versions of the book easily. That’s where Git comes in.

With Git, you can create a “repository” which is like a folder that keeps track of all the changes you make to your files over time. Every time you make a change to a file, you “commit” that change to the repository with a message describing what you changed. This creates a snapshot of the file at that point in time, and you can go back to any previous version of the file whenever you want.

Git also makes it easy to collaborate with other people. You can “clone” a repository from someone else’s computer, make changes to the files, and then “push” those changes back to the original repository. If someone else has made changes to the same file in the meantime, Git will help you merge your changes with theirs so that everyone’s changes are kept intact.

Git is a tool for tracking changes to files over time, making it easy to collaborate with others and keep a history of your work. It’s like a time machine for your files!

Git Commands

  1. git init - Initializes a new Git repository in the current directory.

    1
    2
    3
    
    $ git init
    
    Initialized empty Git repository in /home/send2manoo/Git-demo/.git/
    
  2. git clone - Copies a remote repository to your local machine.

    1
    
    $ git clone https://github.com/user/repo.git
    
  3. git add - Stages changes for commit.

    1
    
    $ git add file1.txt
    
  4. git commit - Commits changes to the local repository.

    1
    
    $ git commit -m "Added a new feature"
    
  5. git push - Pushes changes to a remote repository.

    1
    
    $ git push origin master
    
  6. git pull - Fetches changes from a remote repository and merges them into the local repository.

    1
    
    $ git pull origin master
    
  7. git status - Shows the current status of the repository.

    1
    2
    3
    4
    5
    6
    7
    
    $ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   file1.txt
    
  8. git branch - Lists all branches in the repository.

    1
    2
    3
    
    $ git branch
    * master
      feature-branch
    
  9. git checkout - Switches to a different branch.

    1
    2
    
    $ git checkout feature-branch
    Switched to branch 'feature-branch'
    
  10. git merge - Merges changes from one branch into another.

    1
    
    $ git merge feature-branch
    
  11. git log - Shows the commit history of the repository.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    $ git log
    commit a7d012f96249991e8c6dcfd1fa54f96994c4b4a8
    Author: John Doe <[email protected]>
    Date:   Mon Apr 19 10:24:41 2023 -0700
    
        Added a new feature
    
    commit 98c19e0dc37a05c36473644114e77cc3b3d3a35c
    Author: John Doe <[email protected]>
    Date:   Fri Apr 16 15:47:09 2023 -0700
    
        Fixed a bug
    
  12. git diff - Shows the differences between the working directory and the repository.

    1
    2
    3
    4
    5
    6
    7
    8
    
    $ git diff
    diff --git a/file1.txt b/file1.txt
    index 309b62a..1ce2db4 100644
    --- a/file1.txt
    +++ b/file1.txt
    @@ -1 +1,2 @@
     This is some text.
    +And this is some more text.
    

Summary

Git is a version control system that helps you track changes to your files over time. It’s free, open-source, and widely used in software development and other fields. With Git, you can collaborate with others, maintain a history of your work, and easily revert to previous versions if needed. It’s a powerful tool that can improve your productivity and workflow.

This post is licensed under CC BY 4.0 by the author.

-

Getting Started With Machine Learning