In this blog post, I will continue to explain some of Git basic commands, which I introduced in my previous post. You can find the link to that post here.
Initializing a Git Repository
Initializing a git repository is the first step to use git as a version control system for your project. A git repository is a directory that contains the history of your files and changes. To initialize a git repository, you need to have git installed on your computer and use the command line to navigate to the directory where you want to create the repository. Then, you can use the command git init
to initialize an empty repository. Alternatively, you can use the command git clone
to copy an existing repository from a remote source, such as GitHub.
# Initialize an empty repository
git init
This command creates a new hidden sub-directory named .git that contains all of your necessary repository files.
Checking the Status of the Repository
Git status is a command that shows the current state of your git repository. It tells you which files are staged, modified, untracked, or ignored by git. It also shows you which branch you are on, and if you have any commits or merges to push or pull. Git status is useful for checking the progress of your work and preparing for a commit. To use git status, you can simply type git status
in the command line and press enter.
# Check the status of the repository
git status
# Output:
# On branch main
#
# No commits yet
#
# nothing to commit (create/copy files and use "git add" to track)
Adding Files to the Staging Area
Staging files in git means adding them to the index, which is a list of files that will be committed in the next commit. Staging files allows you to selectively choose which files or changes you want to include in a commit, and also to review and modify them before committing. To stage files in git, you can use the command git add
followed by the name of the file or directory. You can also use the option -A
or --all
to add all the files that have been modified or created.
# Add a file to the staging area
git add <file_name>
# Check the status of the repository
git status
# Output:
# On branch master
#
# No commits yet
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: <file_name>
#
Committing Files
Git commit is a command that records the changes you have made to your files in the git repository. A commit is like a snapshot of your project at a certain point in time. It also contains a message that describes what you have done and why. To make a commit, you need to have some files staged in the index, and then use the command git commit
followed by the option -m
and a message in quotation marks.
# Make a commit with a message
git commit -m "Added new file"
# Output:
# [master (root-commit) 6f7a2b4] Add <file_name> file
# 1 file changed, 1 insertion(+)
# create mode 100644 <file_name>
Checking the Commit History
Commit history in git is a record of all the commits you have made in your git repository. It shows you the changes you have made to your files, the messages you have written, the authors and dates of the commits, and the branches and tags you have created. Commit history is useful for tracking the progress of your project, reviewing and comparing different versions of your files, and undoing or redoing changes. To view the commit history in git, you can use the command git log
in the command line.
# View the commit history
git log
# Output:
# commit 6f7a2b4f8c7c9d9d1c0a3e2e5b8f0a5c3b6f4c1a (HEAD -> master)
# Author: user <user@example.com>
# Date: Sat Sep 30 10:38:07 2023 +0200
#
# Add <file_name> file
Creating a New Branch
Branches in Git are a way of creating and managing different versions of your code. They allow you to work on multiple features or tasks at the same time, without affecting the main branch or each other. Branches are also useful for collaborating with other developers, testing new ideas, and deploying bug fixes.
To create a branch in Git, you can use the git branch
command, followed by the name of the new branch. To switch to a branch, you can use the git checkout
command, followed by the name of the branch.
To see all the branches in your repository, you can use the git branch --list
command. This will show you which branch you are currently on, and which branches are available. You can also use the git branch -a
command to see all the branches, including the remote ones.
To merge two branches, you can use the git merge
command, followed by the name of the branch you want to merge into the current one. For example, if you are on the master
branch and want to merge the develop
branch into it, you can use git merge develop
. This will combine the changes from both branches and create a new commit on the m
branch.a
in
References
Pro Git Book: https://git-scm.com/book
2 Comments