Please Learn Git
A beginner's guide to Git version control, repository setup, commits, remotes, and workflow essentials.
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
- Install git from git-scm.
- Run
git --versionto verify that it is installed.
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 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 mainto create a default branch calledmain. -
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- Push the changes to the remote repository by running
git push -u origin main. - Check the history of the repository by running
git log --oneline.
Pushing changes to remote repository
git add . # git add file
git commit -m "made a change"
git push origin main- After making changes to the files you can either commit them or stash them.
- Run
git add .to track all the files in the repository. - Commit the changes by running
git commit -am "made a change". This captures a snapshot of the repository at its current stage. - Push the changes to the remote repository by running
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 (
.envin this case) that is to be removed to.gitignoreand 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.
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
- git commands: git documentation
- Pro Git ebook: A book on git
- gitignore.io: generate gitignore files
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?