Mastering Git: A Comprehensive Guide to Version Control

From Initialization to Commit Management and File Reversion Techniques

git init

The git init command initializes a new Git repository in the current directory. It creates a hidden .git folder that stores the version control metadata for tracking changes, enabling you to start managing your project's history. Running this command is typically the first step when setting up a new project for version control.

Configuring Git Username and Email
Git requires a username and email address to associate with your commits. You can set these globally (for all repositories) or locally (for a specific repository).

To set your username and email globally:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

To set your username and email for a specific repository:

git config user.name "Your Name"
git config user.email "your.email@example.com"

To check the current configuration:

git config user.name
git config user.email

The git add command stages changes (new, modified, or deleted files) to the Git index, preparing them for the next commit. It doesn't save changes to the repository but marks them for inclusion in the next commit.

  • Stage a specific file:

      git add file.txt
    
  • Stage multiple files:

      git add file1.txt file2.txt
    
  • Stage all changes in the current directory:

      git add .
    

The git commit command saves the staged changes to the repository's history. It creates a snapshot of the current state of your project and requires a commit message to describe the changes.

git commit -m "Your commit message"

Commit is nothing but version

Lets say on version 1 itself I need to add another file but by mistake I did not, so instead of creating a new commit i can able to add in previous commit itself. To amend the last commit (modify message or include more changes):

git commit --amend

use git log to check how many commit/version you have of your project/

  • If you accidentally stage a file:

      git reset file.txt
    

    From staging area it comes to working area

  • Reset to a specific commit:

      git reset <version1-commit-hash>
    

    The changes introduced by version2 are moved from the commit history back to the working directory (unstaged).

  • Discard all changes and reset the working directory to match the specified commit. here, in working area also you wont have your work

      git reset --hard <version1-commit-hash>
    
    1. Start with a committed file:

       echo "original content" > file.txt
       git add file.txt
       git commit -m "Add file.txt"
      
    2. Modify the file:

       echo "new content" >> file.txt
      
    3. Use git checkout -- file.txt:

       git checkout -- file.txt
      
    4. Result:

      • The file is restored to its original content as committed.

      • Any changes made to the file after the last commit are discarded.

  1. Unstage the file (if staged):

    • First, you need to unstage the file using:

        git reset hi.txt
      

      This will remove the file from the staging area and leave it in the working directory with the changes.

  2. Discard the changes after unstaging:

    • Now that the file is in the working directory, you can use:

        git checkout -- hi.txt
      

      This will revert hi.txt to its state from the last commit.

      • git checkout -- <file> only works to discard changes in the working directory if the file is modified but not staged.

      • If the file is staged, you need to unstage it first using git reset <file>, then you can use git checkout -- <file> to revert the file to the last committed state.

                git checkout <version2-commit-hash>

This way you can switch to any version, and you will be on that particular version above versions you wont have. now you wont be having version3,4,5 anything above version2 because version3,4,5,etc are not a part of version2

                git checkout <version1-commit-hash> index.html/folder

Lets say we are on version7 and in version7 we need version1 contents(index.html), so by above command in version7 only index.html will be from of version1.