If you have ever lost important changes in your project or faced the need to revert to a previous version of the code, you are familiar with all the delights of the headache associated with this.
Previously, many developers had to manually create copies of their projects and manage versions by uploading the project folder to a local server. It was time-consuming and time-consuming. However, with the advent of Git, this problem is solved in a few simple steps.
Git is a great tool that allows you to track all changes in a project and easily revert to any version of it without creating copies directly.
We have a task ahead of us: we are developing a project in Visual Studio Code and periodically need to create copies of the entire project. We also have a need to go back to a certain version of the project and continue working with it. How can I implement this task on a local system using Git?
The steps for working with Git in a project are to create copies of versions and return to them if necessary
Pre-installation of Git:
Before we start, let's make sure that Git is installed on our system. We can download it from the official website git-scm.com and follow the installation instructions for our operating system.
Initializing Git:
Let's open the terminal in VSCode in the folder of our project. Initializing a new Git repository:
git init
Adding Files:
Let's create several files in our project folder (for example, index.html
, style.css
and script.js
). Let's add these files to the index:
git add .
Commit changes:
Let's fix the changes with a message:
git commit -m "Initial version of the project"
Working on the project:
Let's make changes to our files. For example, let's change the text in index.html
.
Creating a new commit:
Let's save the changes:
git add .
git commit -m "Changes in index.html "
Saving additional versions:
Let's continue making changes and creating commits:
git add .
git commit -m "Added a new style to style.css"
Viewing the commit history:
To see which commits we have made, use:
git log
This will show a list of our commits with their IDs (for example, abc1234
).
Return to the previous commit:
If we want to go back to the previous commit, use:
git checkout
For example:
git checkout abc1234
After that, our project will return to the state saved in that commit.
Return to the current version:
To return to the last commit, run:
git checkout main # or master depending on our main branch
Make a specific commit the main version of the project:
If we want to make the state of our project after working in a certain commit the main version, we will perform the following steps:
- Switch to the desired commit using the command:
git checkout abc1234
- Let's create a new branch from this commit if we want to save the changes:
git checkout -b new-branch-name
- Let's make the necessary changes and fix them:
git add .
git commit -m "Description of changes"
- Let's switch to the main branch:
git checkout main
- Merge the changes from the new branch into the main branch:
git merge new-branch-name
This way, we can make the project state saved in a specific commit relevant to the work, while keeping all previous versions in the Git history.
Sample work scenario
The first commit:
We have created a index.html
and made a commit with the message "Initial version of the project".
Making changes:
We have changed the index.html
and made a commit with the message "Changes in index.html ".
Creating a new version:
We added styles and made a commit, fixing the changes.
Return to the previous version:
We decided that changes to style.css
are not necessary, and we want to return to the previous commit. Execute git log
, find the commit id and execute git checkout
.
Next, we either work with a separate commit, or create a new branch from it (which is a more correct approach) and work in it already. After that, if necessary, we can merge the new branch with the main one.
This way, we can quickly roll back to the desired version of the project in case of unforeseen situations and continue working with the already up-to-date version.