What is Git and how do Git Commit Commands work? 🖥️
When I started learning git, I felt like I was learning pointless commands: git add
, git commit
, and git push
. I was learning what to do without understanding the bigger picture of what I was inputting.
Hence, in this article, I want to give a better understanding of what is actually going on with Git, as I hoping when I was first starting out. I will discuss git, why we use it, and how to use it best with git commands. I will also explain what happens in the background of a git command.
What is Git?
Git is a distributed version control system (or short VCS):
-
Version Control System (VCS): As the name suggests, a VCS system (or program) lets you control and check every change and version of your code and configuration.
-
Distributed: Distribution played a large part in why 95% of developers reported using Git as their primary version control system. The distributed nature of Git makes collaboration in developer teams more flexible, offering teams a wide array of workflow possibilities. For example, this distribution allows a team of 10 developers to work simultaneously, each locally on their own computer, while being able to review each other's work. Here are more examples of the power of distribution.
Why use Git?
Simply put, git keeps track of all the changes made to your files, giving you complete control over all versions of your code base and the ability to go back to any previous version at any time. This is an extremely powerful position, as it gives you three benefits:
-
Safety: Everything that is stored in git can be accessed at a later time. A record of your saved history is constantly kept. As a result, it is difficult to corrupt your code in such a setup.
-
Distribution: Thanks to distributed development, multiple individuals can work simultaneously on the same project. Working on the same file as someone else would be impossible without version control since each of your modifications would overwrite the other's code when it was committed.
-
Non-linear development: You may want to work on several system components at once. As a result, you are not required to develop apps in a particular order. Perhaps you are fixing a fault in the back-end while working on your system's user interface. With no effort, you can save the work you're working on, address the problem, and go back to your earlier work.
Example of Git
A typical use case of git for Indie Devs is pushing their codebase on GitHub. GitHub is the go-to developer platform that helps you to write, save, and manage your code using Git.
Git is not the same as Github. Git is a version control program, and Github, one of the multiple hosting services for Git repositories, provides all of the Git source code management features. You upload your Git repository to GitHub.
Here is a quick introduction to how it is done in Github:
-
Go to your GitHub profile and select the plus sign (”+”) on the right. Then click ”New repository” to create a new repository. A repository (or short repo) is where all the files of your projects get stored.
-
Give it a repository name, fill out a description, choose whether you want a public (e.g., open-source) or private repository, and click “Create repository.”
-
Now, you have successfully started a project managed by a git system. If you edit the codebase on this repository and commit (i.e. save) the changes to your repository, you will quickly see how git works.
Useful Terminology:
Git: Git itself can have different meanings according to its infamous creator, Linus Torvalds, but I think an abbreviation for “Global Information Tracker” is the most fitting term.
Commit: A commit is a snapshot of changes made to files within a repository at a specific time. This is where you are able to navigate through each version (i.e. commit) and see the changes.
Repository (Repo): A repo is a data structure that stores your files and directories of a specific project and the history of changes made to those files. It serves as the central location for your version control.
Cloning: Cloning is creating a local copy of a remote repository. This copy includes all the files, directories, commit history, and branches in the remote repository. Cloning allows developers to work on the project locally, make changes, and contribute to the original repository through push and pull operations. git clone <Repository URL or SSH>
Forking: Forking is creating a personal copy of someone else's repository under your GitHub account. This copy is independent of the original repository and allows you to make changes without affecting the original project. Forking is commonly used to contribute to open-source projects or experiment with modifications while keeping the original repository intact.
Stages: When you save a file, Git does not automatically add changes as versions to the repository. Instead, you must actively inform Git that the change belongs to a new version, and it must explicitly go through three local phases before it is registered in the repository:
-
Working Directory: When you edit and save your code locally. This is also referred to as the "untracked" region in git since you will lose any changes made if you do not actively save it to git. When you enter the command
git status
, you can see which working tree has been edited but not yet saved to the repository. -
Staging area: Staging pushes the updated local modifications from the working tree to the staging area and keeps track of them. It is where all changes are recorded but not yet committed to the actual code base. (
git add .
) -
Local repository: This is the final saving of your changes as a version. We can only commit changes from the staging area into this stage, never from the working tree. (
git commit -m 'ex. Frederic made a commit'
) -
Remote repository: This can be your GitHub Repository that stores all the Git changes on the server. (
git push origin main
)
Git Commands Cheat Sheet
As described in the terminal article, I am a big believer in the helpfulness of personalised cheat sheets. In this sense, I have listed applicable git commands below. You can copy, expand, and personalise your own git command cheat sheet. Feel free to share your expanded list with me. Read here if you are interested in my full terminal command cheat sheet.
# Initialize a new Git repository in the current directory
git init
# Clone a repository into a new directory
# Replace <SSH repository> with your actual SSH URL (e.g., git@github.com:ownerName/projectName.git)
git clone <SSH repository (ex. git@github.com:ownerName/projectName.git)>
# Add changes to a specific file to the staging area
# Replace <file> with your file path (e.g., docs/example.txt)
git add <file (ex. docs/example.txt)>
# Add all changes to the staging area
git add .
# Commit changes in the staging area with a message
git commit -m "Frederic made this change"
# Show the status of changes as untracked, modified, or staged
git status
# (Optional) Shell alias for short status (requires setup)
gss
# Display the commit history
git log
# List all local branches with the current branch highlighted
git branch
# Create a new branch
# Replace <branch_name> with your desired branch name
git branch <branch_name>
# Switch to a different branch
git checkout <branch_name>
# Merge changes from another branch into the current branch
git merge <branch_name>
# Fetch changes from a remote repository and merge them into the current branch
git pull
# Push local changes to a remote repository
git push
# Show a list of remote repositories and their URLs
git remote -v
# Fetch changes from a remote repository
git fetch
# Unstage changes for a file
git reset <file>
# Remove a file from the working directory and the repository
git rm <file>
# Show the differences between the working directory and the staging area
git diff
# Show the differences between two commits
git diff <commit1> <commit2>
# Create a lightweight tag for the current commit
git tag <tag_name>
# Add a remote repository
git remote add <name> <url>
# Pull changes from a specific branch of the remote repository
git pull origin <branch>
# Push changes to a specific branch of the remote repository
git push origin <branch>
# Configure the global user name and email address for identification in commits
git config --global user.name "Your Name"
git config --global user.email "frederic@InternetGarden.co"
# Move or rename a file, a directory, or a symlink
git mv
# Restore working tree files
git restore
# Need more help? Enter the command and learn directly from the terminal
git help --all
Want to study more about git commands? Read more here: Git Book and Version Control with Git
Here is also a complete list of all git commands: All Git Commands
Lastly, never forget to commit often, or you might run into the risk of losing code. ⚠️
Conclusion
Understanding Git goes beyond memorizing commands; it is about grasping its fundamental purpose. Git is a distributed version control system that provides developers with the seamless possibility to track changes and collaborate. It is an essential tool for Indie Devs to create, manage, and deploy software faster.
That is it, and I hope you found it helpful. Please let me know how I could improve this article. 🙏