Please learn git

A guide to git for beginners

November 13, 2020 (5y ago)

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

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 it's 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 and create a new repository.

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

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

Pushing changes to remote repository

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.

git commit --allow-empty -m ''

Removing a file from the repository

Removing .env from the repository and the history.

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'll have to filter it out of all the branches using git filter-branch.
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 force push the changes to the remote repository using git push --force.

You can also have a look at the example show 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?