Table of contents
In the vast world of software development, version control is like a guiding star that keeps your coding journey on track. Enter Git, your trusty intergalactic companion, here to make version control as simple as counting to three! ๐๐ธ
So, git, which is used to track the changes in your code is as easy as sharing memes. So just knowing the basic command it will make your work easy. Let's learn some basic commands that will kick-start your git journey.
1. git init - The git init command is the first command that you will run on Git. The git init command is used to create a new blank repository. It is used to make an existing project as a Git project. Several Git commands run inside the repository, but the init command can be run outside of the repository.
The git init command creates a .git
subdirectory in the current working directory. This newly created subdirectory contains all of the necessary metadata. These metadata can be categorized into objects, refs, and temp files. It also initializes a HEAD pointer for the master branch of the repository.
Let's create a new folder and then use the git init command. git init
2. git add - The git add command is used to add file contents to the Index (Staging Area). This command updates the current content of the working tree to the staging area. It also prepares the staged content for the next commit.
The git add command is a core part of Git technology. It typically adds one file at a time, but there some options are available that can add more than one file at once.
The git add command can be run many times before making a commit. These all add operations can be put under one commit. The add command adds the files that are specified on the command line.
Let's create a file and then try to add the file to the staging area.
git add filename
3. git status - The git status command is used to display the state of the repository and staging area. It allows us to see the tracked, untracked files and changes. This command will not show any commit records or information.
Mostly, it is used to display the state between Git Add and Git commit commands. We can check whether the changes and files are tracked or not.
git status
As you can see we have added the file that is showing in green color. You can also see that it is showing no commits yet
. So let's commit this with the commit command.
4. git commit - It is used to record the changes in the repository. It is the next command after the git add. Every commit contains the index data and the commit message. Every commit forms a parent-child relationship. When we add a file in Git, it will take place in the staging area. A commit command is used to fetch updates from the staging area to the repository.
Commits are the snapshots of the project. Every commit is recorded in the master branch of the repository. We can recall the commits or revert it to the older version. Two different commits will never overwrite because each commit has its commit-id.
let's commit to the changes. git commit -m "commit"
As you can see we have created a new commit and it also shows the 1 file changed.
5. git log - The advantage of a version control system is that it records changes. These records allow us to retrieve the data like commits, figuring out bugs, and updates. But, all of this history will be useless if we cannot navigate it. At this point, we need the git log command.
Git log is a utility tool to review and read the history of everything that happens to a repository. Multiple options can be used with a git log to make history more specific.
git log
6. git push - The push term refers to uploading local repository content to a remote repository. Pushing is an act of transferring commits from your local repository to a remote repository. Pushing is capable of overwriting changes; caution should be taken when pushing.
we can say the push updates the remote refs with local refs. Every time you push into the repository, it is updated with some interesting changes that you made. If we do not specify the location of a repository, then it will push to the default location at origin main.
git push -u origin main
here I'm using another repo to show you how you can push our commits to a remote repo.
7. git branch - The command git branch is used to check on which branch you are and it will also show you how main different branches are also available. It is used to list, rename, and delete branches within a Git repository
I have just one branch which is the main so it is showing the main only.
8. git checkout - The git checkout
command in Git is used to switch between different branches, commit points, or files in a Git repository. It's a versatile command that allows you to navigate through the history of your project and work on different branches or commit states.
To switch to an existing branch, you use the following syntax:
git checkout <branch_name>
9. git pull - The git pull
command in Git is used to fetch and integrate changes from a remote repository into your current local branch. It's essentially a combination of two other Git commands: git fetch
and git merge
.
When you run git pull
, Git will first fetch any new changes from the remote repository that your local repository is tracking. This is done using the git fetch
command.
git pull origin <branch_name>
Here, <branch_name>
is the name of the branch on the remote repository that you want to pull changes from (often main
, master
, or a feature branch).
10. git merge - The git merge
command in Git is used to combine the changes from one branch into another. It's a crucial command for integrating code changes and creating a unified history in your Git repository.
To merge changes from one branch into another, follow this syntax:
git merge <source_branch>
Here, <source_branch>
is the branch that contains the changes you want to merge into the current branch.
The End
Thank you for taking the time to read my blog post. If you enjoyed what you read, don't forget to share it with others and subscribe to my blog newsletter for more insightful content in the future! Your support means the world to me.