The Git

Fredd Somm

Fredd Somm

· 8 min read
Person holding a Git sticker.

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. 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):

  1. 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.
  2. 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:

  1. 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.
  2. 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.
  3. 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 Hackers 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:

  1. 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.
  2. 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.”
  3. 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:

  1. 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.
  2. 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.
  3. 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.
  4. 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>
  5. 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.
  6. 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:
  7. When you save your file, 1. 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.
  8. When you enter the command git add ., 2. 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.
  9. When you enter the command git commit -m 'ex. Fredd made a commit', 3. 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.
  10. When you enter the command git push origin main, 4. remote repository: This can be your GitHub Repository that stores all the Git changes on the server.

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.

git init //Initialise a new Git repository in the current directory 

git clone <SSH repository (ex. git@github.com:freddsomm/IndieHacker.git)> //Clone a repository into a new directory 

git add <file (ex. docs/example.txt)> //Add changes to a specific file to the staging area 

git add . //Add all changes to the staging area 

git commit -m "Fredd made this change" //Commit changes in the staging area with a message 

git status 
#or 
gss //Show the status of changes as untracked, modified or staged 

git log //Display the commit history 

git branch //List all local branches with the current branch highlighted 

git branch <branch_name> //Create a new branch 

git checkout <branch_name> //Switch to a different branch 

git merge <branch_name> //Merge changes from another branch into the current branch git pull Fetch changes from a remote repository and merges them into the current branch 

git push //Push local changes to a remote repository git remote -v Show a list of remote repositories and their URLs 

git fetch //Fetch changes from a remote repository 

git reset <file> //Unstaged changes for a file 

git rm <file> //Remove a file from the working directory and the repository 

git diff //Show the differences between the working directory and the staging area 

git diff <commit1><commit2> //Show the differences between two commits 

git tag <tag_name> //Create a lightweight tag for the current commit 

git remote add <name><url> //Add a remote repository 

git pull origin <branch> //Pull changes from a specific branch of the remote repository 

git push origin <branch> //Push changes to a specific branch of the remote repository 

git config --global user.name "Your Name"
git config --global user.email "freddsomm@IndieHacker.Info" //It configures the global user name and email address for identification in commits. 

git mv //Move or rename a file, a directory, or a symlink 

git restore //Restore working tree files 

git help --all //Need more help? Enter the command and learn directly from the terminal

Want to study more about git commands? Read more here: https://git-scm.com/book/en/v2 and https://www.oreilly.com/library/view/version-control-with/9781492091189/

Here is also a complete list of all git commands: https://mirrors.edge.kernel.org/pub/software/scm/git/docs/

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 Hackers 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.

Fredd Somm

About Fredd Somm

I deeply care about creating exceptional digital experiences but come from a non-technical background.

IndieHacker.Info is the guide I wished I had to start my own SaaS company as a total beginner. Pre-order the Notion Template "From 0 to IndieHacker" now!

Made with ❤️ by FreddSomm.com
Copyright © 2024 IndieHacker.Info | Subscribe to my newsletter here