← Back to Articles List

Guide to Git

How to get your project on Github
1. Open command prompt in your project directory and run the command "git init". Next run "git add ." and "git commit" respectively.
    *** if gitignore is needed for your repo, creating ".gitignore" must be the first step. It should be done before first commit. To remain on the safe side practice adding gitignore even before "git init" command initialization because once a file is commited gitignore cannot remove it automatically.
2. Open github on your browser and create a repository with prefered name.
3. Now it is time to link the local repository to the github repository. Run the following commands to achieve it.
    git remote add origin git@github.com/your_username/your_git_repository_name.git (it requires ssh connection) 
    or,
    git remote add origin https://github.com:your_username/your_git_repository_name.git (to connect over HTTPS with username & PAT)
    git branch -M main
    git push -u origin main

How to create a new branch
Use the command "git branch new-branch-name" to create a new branch in your git repository.

How to switch to a branch
Use the command "git checkout branch-name" to switch to that branch.

git init
This is the very first command one should know to work with git. This command initializes an Git reposity. Meaning it turns your current directory into a version controllable GIT repository. By running this command, you are telling Git to keep track of all the changes made in the current directory.
git checkout -b new-branch-name
This git command creates a branch called new-branch-name and automatically switches you to the newly created branch. While using this command use what name you prefer for your new branch instead of new-branch-name.
git add .
Stages all changes in the current directory and its subdirectories. Meaning it prepares the changes to be saved in the git. Next step is commiting.
git commit -m "comment"
Saves staged changes to local repository. The -m flag allows adding a short, descriptive message directly from the command line, which serves as a comment for the commit.
git push or git push -u origin branch-name
git push command uploads your local commits to a remote repository (Github, Gitlab, or any other remote repostory). 
**When pushing files on a newly created branch for the first time, git push -u origin branch-name must be used to avoid error. -u sets the upstream. It creates the branch on your origin meaning remote/git repository and sets a link between the local branch and the new remote branch. Afterwards only git push command is enough.
git checkout -b new-branch-name
This git command creates a branch called new-branch-name and automatically switches you to the newly created branch. While using this command use what name you prefer for your new branch instead of new-branch-name.
git add .
Stages all changes in the current directory and its subdirectories. Meaning it prepares the changes to be saved in the git. Next step is commiting.
git commit -m "comment"
Saves staged changes to local repository. The -m flag allows adding a short, descriptive message directly from the command line, which serves as a comment for the commit.
git push or git push -u origin branch-name
git push command uploads your local commits to a remote repository (Github, Gitlab, or any other remote repostory). 
**When pushing files on a newly created branch for the first time, git push -u origin branch-name must be used to avoid error. -u sets the upstream. It creates the branch on your origin meaning remote/git repository and sets a link between the local branch and the new remote branch. Afterwards only git push command is enough.

Merge a Branch to Main
It is considered to be the best practice that you merge the branch with main after implementing a feature successfully. It ensures that you have a production ready main branch all the time. You have to use several git commands step by step which is given below.
1. git checkout main to switch into main branch.
2. git pull origin main to get the latest changes made into main branch provided you are working collaboratively.
3. git merge feature-branch to merge the feature-branch with main branch.
4. git push origin main to push the updated main branch to the remote repository.
5. git branch -d feature-branch to delete the feature-branch from local repository.
6. git push origin --delete feature-branch to delete the feature-branch from local repository.

Discard all unstaged changes
Use the command git checkout . to revert the files in your working directory back to the state of the last commit.