Git cheat sheet for beginners
Let’s start with basic commands for git.
Clone a repository
This is a sample github repository: https://github.com/nisalap/github-tutorial
For private repositories, to use SSH, you need to add the SSH key as in [Git Bas For private repositories, with https, the git tool will ask for username and password for your account to access the repository.
Git init
If you haven’t cloned a repository then go the directory which contains files and on a terminal run:
git init
Fork a repository
Fork does not involve copying the repository to your local machine. Instead you will be creating a copy of the repository on Github. Forking does not involve any commands. You can fork a repository by clicking on the fork button on Github web.
Clone vs Fork
Forking just creates a copy of your code on the Github. If you plan on editing your code on the local machine clone is a must. So you can either fork a repository and then clone your fork to the local machine or you can directly clone a repository on Github
Git commit
Let’s say you have some code changes that you need to add. To add these changes, you need to first stage the changes that you have done. Staging the changes makes these changes ready to be committed.
To commit these changes run the following command. Replace Commit message with a Now you have successfully committed the changes. Still these changes are in your local machine. It is time to upload them.
Git push
Push command uploads the changes to a given remote repository. In order to push the changes you need to setup a Git remote.
Now that you have set the remote, you can push the commits. git push origin main
Git branch
To see the different branches on a given repository, run:
git branch
Git checkout
To change the branch, use git checkout:
If you want to checkout to a new branch, git checkout -b new_branch_name
Git tracking branch/ upstream
Tracking branch (upstream) refers to the branch on the remote that you need to push the local branch to.