Master Git Basics: Create Repositories, Commit, and Recover Versions
This guide explains Git's origins, compares distributed and centralized version control, walks through creating a repository, adding and committing files, switching versions, and using reset, log, and reflog to recover previous states, providing practical command‑line examples for developers.
Git was created in 2005 by Linus Torvalds to address the limitations of centralized version control systems like CVS and SVN, aiming for speed, simple design, strong support for non‑linear development, full distribution, and the ability to manage large projects such as the Linux kernel.
Distributed vs. Centralized
Centralized systems store the repository on a single server, requiring network access for every operation, which can be problematic in low‑bandwidth environments. In contrast, Git is distributed: each developer has a complete copy of the repository, allowing work offline and easy sharing of changes.
Creating a Repository
A Git repository is simply a directory that Git tracks. To create one:
<code>mkdir experiment<br/>cd experiment<br/>git init</code>This initializes an empty repository and creates a hidden .git folder that should not be modified manually.
Adding and Committing Files
Example workflow:
<code>echo "This is a test." > hello.txt<br/>git add hello.txt<br/>git commit -m "first create"</code>The -m flag provides a commit message; the output shows one file changed with one insertion.
Switching Versions
After several commits, you can view the history with:
<code>git log</code>The log displays commit IDs, authors, dates, and messages. To revert to a previous commit (e.g., the second modify), use:
<code>git reset --hard 46e011a</code>This restores the working directory to the state of that commit.
Recovering Lost Commits
If a commit seems missing after a reset, git reflog shows all recent actions, including the discarded commit IDs, allowing you to recover them.
<code>git reflog</code>The reflog output lists entries such as fab1858 HEAD@{1}: commit: third modify , from which you can retrieve the desired commit.
360 Zhihui Cloud Developer
360 Zhihui Cloud is an enterprise open service platform that aims to "aggregate data value and empower an intelligent future," leveraging 360's extensive product and technology resources to deliver platform services to customers.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.