Skip to main content

Command Palette

Search for a command to run...

Git (Part 2)

Updated
4 min read

In my previous session, I focused on the fundamentals of Git such as creating repositories, staging files, committing changes, and understanding the basic workflow.
Today, I explored some advanced but very practical Git concepts: recovering deleted files, using stash to save unfinished work, and working with GitHub for remote collaboration. These concepts are directly relevant to real-world development and teamwork.


Git Restore – Recovering Deleted Files

The git restore command helps in recovering files that have been deleted. The approach depends on the stage at which the file was deleted.

Scenarios:

  1. Deleted but not staged

     git restore <filename>
    

    This will bring back the file that was deleted in the working directory.

  2. Deleted and staged for commit

     git restore --staged <filename>
     git restore <filename>
    

    First, remove it from staging, then restore it back.

  3. Deleted, staged, and committed

     git log
     git checkout <commitID> -- <filename>
    

    Check the commit history, identify the commit where the file existed, and restore it.

  4. File name unknown

     git log --stat
    

    This shows the list of files modified in each commit, making it easier to identify and recover.

Example:
If config.json was deleted and committed, I can recover it with:

git checkout 7g34h2a -- config.json

Git Stash – Saving Work in Progress

Sometimes we start working on a task that cannot be committed yet because it is incomplete. If we suddenly need to switch to another task, Git provides the stash functionality to temporarily save the work.

Workflow Example:

  • At 10:10 AM, I start working on a new feature. The code is half done.

  • At 11:30 AM, my team lead asks me to work on another urgent fix.

Instead of committing incomplete code, I use stash:

git stash

The changes are now stored safely, and my working directory is clean.

Useful Commands:

  • git stash list → View stashed changes.

  • git stash apply → Apply the most recent stash.

  • git stash apply stash@{2} → Apply a specific stash.

This allows me to complete the urgent fix, then later reapply my previous work.


GitHub – Remote Collaboration

GitHub is a remote hosting platform for Git repositories. It enables collaboration between developers, code sharing, and version control in the cloud.

Key Points:

  • Free signup with 2 GB storage for free tier accounts.

  • Supports two types of repositories:

    • Public repositories – accessible by anyone.

    • Private repositories – accessible only to authorized users.

  • Every repository has a unique URL, for example:

      https://github.com/username/project-repo.git
    

Working with GitHub

1. Clone a repository

git clone <repoURL>

2. Create a file locally, add and commit

git add .
git commit -m "Added new feature"

3. Push changes to remote

git push -u origin main

(Requires configuration with a Personal Access Token for authentication.)

4. Pull changes from remote

git pull

This ensures the local branch is updated with the latest changes from the remote repository.


Real-World Collaboration Example

  • Developer 1 clones the repository at 10:00 AM, creates a new file, commits it, and tries to push at 10:30 AM.

  • Developer 2 directly creates a new file in the GitHub repository at 10:20 AM.

  • When Developer 1 pushes at 10:30 AM, Git prevents the push because the remote has new changes.

  • Developer 1 must first run git pull, resolve conflicts if any, and then push.

This process ensures that no one’s work is overwritten.


Forking a Repository

Forking allows us to create a copy of someone else’s repository into our own GitHub account.

  • Useful for learning from open-source projects.

  • Common in contributing to community projects, where changes are first made in the forked repository and later proposed to the original repository via pull requests.


Key Takeaways

  • git restore is a reliable way to recover deleted files depending on the stage of deletion.

  • git stash is essential for switching tasks without losing ongoing work.

  • GitHub provides a structured way to collaborate remotely on projects.

  • Forking enables working on external projects and contributing to open source.

Next, I will move on to exploring Git branching and merging, which is crucial for parallel development and team collaboration.

More from this blog

Documenting My DevOps Journey

40 posts