Please Learn Git

A beginner's guide to Git version control, repository setup, commits, remotes, and workflow essentials.

Ram Shankar
3 min read
#Git#VersionControl#Workflow#Beginners

This essential read will illuminate the path to version control and equip you with the knowledge you need to elevate your coding game.

Installing Git

Creating a new repository with GitHub

shell
git init
git add . # tracks all the files
git status
git commit -m "commit message here"
git status
git branch -M main
git remote add origin git@github.com:my_username/my_repo_name.git
git push -u origin main
  • Initialize a git repository, run git init.

  • Track all the files in the repository by running git add ..

  • Commit the changes by running git commit -am "commit message here". This captures a snapshot of the repository at its current stage.

  • Push the changes to the remote repository by running git push origin main.

  • Check the status of the repository by running git status. If you run this command before committing, you will see a list of files that have been added, modified, or deleted.

  • We now have to connect to a remote repository, which is basically a URL to a repository that can be accessed by other people. There are many cloud-based hosting services that let you manage Git repositories, GitHub being a popular choice. To do this, create a new repository in your GitHub account.

  • Run git branch -M main to create a default branch called main.

  • Add a remote repository to your local repository. To do this run git remote add origin <remote url>.

shell
git remote add origin git@github.com:<GITHUB_USERNAME>/<GITHUB_REPOSITORY_NAME>.git

Pushing changes to remote repository

shell
git add . # git add file
git commit -m "made a change"
git push origin main

Commit without message

Git by default does not allow us to commit without a message. To do this we can use the --allow-empty option:

shell
git commit --allow-empty -m ''

Removing a file from the repository

Removing .env from the repository and the history:

shell
git rm -r --cached .env
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch .env" HEAD
git push --force
  • Add the file (.env in this case) that is to be removed to .gitignore and remove it from git using git rm.
  • The file (.env) still exists in our history and the contents are still visible. We will have to filter it out of all the branches using git filter-branch.
shell
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch .env" HEAD
  • Since we are changing the history of both local and remote repository, we have to force push the changes to the remote repository using git push --force.

You can also have a look at the example shown in git docs.

Glossary

git stash: It temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them. It enables you to switch branches without committing the current branch.

Further reading

Some useful questions to think about

  • How do I remove a file from the repository and the history?
  • How do I get new branch from the remote repository that I have forked?
  • What is the difference between git pull and git fetch?
  • What should I do if my code is 2 commits behind HEAD?

Portfolio search

Search pages, projects, writing, and change the color theme.