Get Good with Git: Part 3 - Basic Git commands
In Part 3, we will explore how to use Git for version control by covering the following topics: initializing a repository, adding and committing files, viewing commit history, creating and switching branches, and merging branches.
In Part 3, we will explore how to use Git for version control by covering the following topics: initializing a repository, adding and committing files, viewing commit history, creating and switching branches, and merging branches.
Initializing a repository
The first step in using Git for version control is to initialize a new repository. To do this, navigate to the directory where you want to create the repository and run the following command:
git init
This will create a new Git repository in the current directory. You can confirm that the repository was created by running the following command:
la -la
This will list all files and directories in the current directory, including any hidden files. You should see a new directory called ".git" which contains all the Git-related files for this repository.
Adding and committing files
Once you have initialized a new repository, you can start adding files to it. To add a file to the repository, you can use the following command:
git add <filename>
For example, if you have a file called "index.html" in your repository, you can add it to Git using the following command:
git add index.html
After you have added the file, you can commit it to the repository using the following command:
git commit -m "commit message"
The commit message should describe the changes you made in this commit. For example, if you added a new feature to your codebase, your commit message might be "Add new feature to index.html".
Viewing commit history
Once you have made some commits to your repository, you can view the commit history using the following command:
git log
This will display a list of all the commits in the repository, including the commit message, the author, the date, and the commit hash. You can use this information to keep track of changes to your codebase and to revert to previous versions if necessary.
Creating and switching branches
Git allows you to create and switch between different branches in your codebase. This is useful for working on new features or bug fixes without affecting the main codebase. To create a new branch, you can use the following command:
git branch <branchname>
After you have created the new branch, you can switch to it using the following command:
git checkout <branchname>
For example, to switch to the "new-feature" branch, you can use the following command:
git checkout new-feature
Merging branches
Once you have created a new branch and made some changes to it, you can merge it back into the main codebase using the following command:
git merge <branchname>
Git will automatically try to merge the changes from the branch into the main codebase. If there are conflicts, you will need to resolve them manually before you can complete the merge.
In Part 3, we have covered the basics of using Git for version control. We have learned how to initialize a repository, add and commit files, view the commit history, create and switch between branches, and merge branches. By using Git for version control, you can keep track of changes to your codebase and collaborate effectively with other developers on your team.
End of Part 3 of 10