Jacob
Posted on July 6, 2023
I've recently discovered the power of Git Commands, and I must say, it's truly fascinating! What amazes me is the control that Git commands offer over tools like GitHub Desktop or VS code. Today, I'm excited to share some of the commands I've learned, and together, we'll unravel the magic behind them!
Let's Begin with the Basics: What is Git?
In a nutshell, Git is a distributed version control system primarily designed for tracking changes in source code during software development. It empowers multiple developers to collaborate on a project while keeping a record of different versions of the codebase.
Exploring Git's Fundamental Commands
Now, let's dive into a few essential Git commands that will revolutionize the way you manage your projects.
Repository Initialization and Configurations:
-
git config [ options ]
: This command is used to configure Git settings. It allows you to set your name, email, default branch, and other options. For example, you can usegit config --global user.name "John Doe"
andconfig --global user.email "johndoe@gmail.com"
to set your name as "John Doe" and your email asjohndoe@gmail.com
globally in Git. -
git init
: This command initializes a new Git repository in a directory. It sets up the necessary files and folders to start tracking changes. For example, when you create a new project, you can usegit init git-example
to tell Git to start keeping track of all the changes you make. -
git clone <url>
: This command is used to create a copy of a remote Git repository on your local machine. It downloads all the files and the complete history of changes from the remote repository. For example, if your friend has a project, you can usegit clone https://github.com/johndoe/git-example.git
to make an exact copy of it on your own local machine.
Managing Changes:
-
git add <filename>
: This command adds the changes you've made to the staging area in Git. It prepares the changes to be included in the next commit. For example, create a new file by the name index.html this can be can be added to the staging area by usinggit add index.html
. -
git add .
: This command adds all the changes you made to the staging area in Git. -
git status
: This command shows the current status of the Git repository. It tells you which files have been modified, added, or deleted since the last commit. For example, if a new file was createdgit status
would show that a file has been created and if that file is currently been tracked. -
git commit -m "<message>"
: This command creates a new commit in Git, which is a snapshot of the changes you added to the staging area. The commit is given a unique identifier and a message that describes the changes. For example, we created a file called index.html we can commit our file usinggit commit -m "index.html created"
. - Alternatively, you can use
git commit -am "<message>"
: This command combines two actions, staging changesgit add .
and creating a commitgit commit
. The-a
option automatically stages all modified files, while the-m "<message>"
option allows you to provide a commit message directly without opening an editor. It's useful when you don't have new files to add to the staging area. For example, you can usegit commit -am "Updated index.html"
Branching and Merging:
- In Git, a branch is a reference point to a specific commit within a repository's commit history. It allows us to work on different aspects of a project simultaneously, keeping changes isolated from one another until they are ready to be merged.
-
git branch -a
: This command lists all the branches in your Git repository, including both local branches (created on your machine) and remote branches (from other computers or servers). It shows you the different versions of your project that exist. -
git checkout -b <branch_name>
: This command creates a new branch in Git and switches to that branch in one step. It allows you to work on a new feature or fix without affecting the main branch. For example, if you want to work on a new html file, you can usegit checkout -b new_file
to create a branch called "new_file" and start building your unique html file like about-us.html. -
git merge <branch_name>
: This command combines changes from one branch into another branch. It integrates the changes made in the specified branch into the current branch. For example, if you finished building your new file in the "new_file" branch and want to bring it into the main branch, you can use git merge new_file to merge the changes into the main branch.
Remote Collaboration:
-
git fetch
: This command downloads all the changes and new branches from a remote Git repository to your local machine. It updates your local copy of the repository without merging the changes into your current branch. -
git pull
: This command combines two actions, it fetches the changes from a remote Git repository (like git fetch), and then it automatically merges those changes into your current branch. It updates your local copy and incorporates the changes made by others. -
git push
: This command is used to send your local Git commits to a remote repository. It uploads your changes and makes them available to others who have access to the remote repository.
Inspecting History and Commits:
-
git log
: This command displays a history of all the commits in a Git repository. It shows information such as the commit's unique identifier, author, date, and commit message. -
git show <identifier_id>
: This command displays the detailed information about a specific commit. It shows the changes made in that commit, including the file differences. For examplegit show 5ea9b3b
would show a detailed information on that commit
That's it for now, for a full list of Git commands or Git cheat-sheet you can visit this link:Git-Cheat-Sheet
Thanks For Reading :)
Posted on July 6, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
October 30, 2024