How To Push Project to Git-Hub or Git-Lab

Akarsh Sharat
8 min readApr 17, 2024

--

Step 1: Login To Git-Lab/ Git-Hub and Create Project Repository
For Git-Hub :

> Login
> Click on Repositories from top menu or click on your profile picture and click on “Your Repository”

> Click on new (Right side green button)

> You will see this page

  • Repository name: Name of repo , to identify
  • Description: Here you can describe your project
  • Choose Public or Private :
    Anyone can see, clone , fork in Public repo but only owner and invited members can commit. Where as in in Private repo only owner and invited members can see and commit to it.
    (you can later change this from repo setting)
  • Add ReadMe file : You can choose to add readme file here or later manually if required.
    ( I prefer not to add readme file here , rather write it in local system and push it with project )
  • Add .gitignore : Add file instructing Git to exclude specific files and folders from version control.
    (this is optional and you can add it later if required)
  • Choose a license: This file contains instructions for other users to tell them what can they do and cannot do with this project.
    (Kinda like copy rights , It is optional and can be added later if required)

>Fill everything and Click on Create Repository

Step 1: Login To Git-Lab/ Git-Hub and Create Project Repository
For Git-Lab :

>Login
> Click on Projects in left menu

> click on New Project (blue button on right side)

> Create blank Project
> You will see this page :

  • Project name: Name of project, to identify
  • Project URL : Unique web address for project
    Generally we add the group name in this blank if project is part of a project group. To create new project group you can go to Groups from left menu and create new group.
  • Project Slug: Unique identifier for your project in case you have multiple project with same name.
    (it is auto generated but you can customize it )
  • Project deployment target: specific environment or platform where your application is intended to run or deploy your project.
    ( You can configure it later if required )
  • Choose Public or Private :
    Anyone can see, clone , fork in Public repo but only owner and invited members can commit. Where as in in Private repo only owner and invited members can see and commit to it.
    (you can later change this from repo setting)
  • Add ReadMe file : You can choose to add readme file here or later manually if required.
    ( I prefer not to add readme file here , rather write it in local system and push it with project )
  • Enable Static Application Security Testing (SAST): It automatically scan your code for security vulnerabilities as part of your development workflow
    ( It is optional and can be enabled later if required)

>Fill everything and Click on Create Project

Step2: Clone Repo to local and configure it for push/ pull
This is same for both

We need Git to connect remotely to Git-Hub or Git-Lab. Git comes installed by default on most Mac and Linux machines.

To check type this command in terminal :

git version

If you are using Windows Machine or if you don’t have Git installed in your Linux or Mac Machines, easiest method is to install Git-Bash.

Go to below link ad download it for your OS, in this case
Downloads > Windows > 64-bit Git for Windows Setup.

https://git-scm.com/downloads

> Run setup as administrator then click yes
> Click Install
> Leave everything at default and click next
> Click Install
> Check Run Git-bash & Click Finish
> In Git-bash Terminal Type

git version

Output : (This means git is successfully installed)
git version 2.44.0.windows.1

> Now go to your Project directory using ‘cd’ command or if you are on windows you can just go to your project directory using file explorer, then right click on your project folder and select open git bash here.

> Now, Once you are in project folder in terminal you can either clone a repo in this folder or push your local project to your git-hub repo.
(These commands are same for Windows, Linux and Mac)

Clone Repository:

Copy the URL of repository you want to clone.
It looks something like this : https://github.com/<Github-UserName>/<RepositoryName>.git

> Go to your git-bash terminal

git clone <URL>

this will clone repo to your local machine. But if the repo is private, it will ask for git-hub UserName and Password for authorization.

Push Local project to Git-Hub/ Git-Lab:

Go to your Project Folder in git-bash terminal

ls

you will see all the files in your project folder.

git init
git status

this will show something like this :

Now we have to add files we want to commit (files we want to push to git-hub)

  • To add all files
git add .
  • To add specific files
git add <file1> <file2> ...

Check status again

git status

Now you can see :

all the files are added to tree.

Commit changes

git commit -m "<Comment>"
git remote add origin <repo URL>
git push origin -u origin main 

It will ask for user.name and password, If you want to set your username, email and password to global so that you don’t have to enter userName and password every-time —

git config --global user.name "<enter your user name>"
git config --global user.email "<enter your email>"
git config --global user.password "<enter your password>"

Just to check if everthing is saved in global or not ?

git config --global 

Output will be something like this :

$ git config --global --list
user.name=YourUserName
user.email=YourEmail
user.password=YourPassword

Now you have configured your global variable, so next time you need user and password it will automatically use these entries.

Now in Case you want to set SSH Access Token for Auth in Git-Lab,
You can follow this link :

Bonus Section :
May be most important section for most :)

To see all branches on local:

git branch

Output is something like this, here * shows active branch (that u r currently in )

To switch branch :

git checkout <name_of_branch>

Output is something like this

To Create New branch :

git checkout -b <new-branch>

Output is something like this

Just to be sure you can type — git branch command and check if it is created or not.

To Create New branch based on Existing Branch:

git checkout -b <new-branch> <existing-branch>

To see remote branches:

git fetch --all 
 git branch -r

Output something like this

To see all branch including remote:

git branch --all

Output something like this

To switch to remote branch:

git checkout <remoteBranchName>

To delete branch:

git branch -d <branchName>

or

git branch --delete <branchName>

Output (Make sure you’re not trying to delete your current branch; switch to a different branch first, then proceed)

To see what commits have been added to the upstream Branch:

 git log --oneline <branchName>..origin/<branchName>

To Fetch and Merge :

//fetch from remote to local//
git fetch origin

//switch to required branch//
git checkout <branchName>

//Check all logs and approve//
git log origin/<branchName>

//Merge to local branch //
git merge origin/<branchName>

To Push to remote branch:

git push <remote> <branch>

To force push : ( This force the push even if it results in a non-fast-forward merge)

git push <remote> --force

* DON’T FORCE IT UNLESS YOU ARE ABSOLUTLY SURE YOU KNOW WHAT YOU ARE DOING*

To remove Untracked-File:
— Untracked files are files that have been created within your repo’s working directory but have not yet been added to the repository’s tracking index using the git add command. —

// Dry run to show which files will be removed //
git clean -n

//force it to clean //
git clean -f

To create an Alias for a command:

git config --global alias.<aliasName> <command>

Output :

Now we can use “checkout” command with only writing “co”.

To remove files/files from repository:

git rm <file1> <file2> ...

To Undo rm:

git reset HEAD

(This will reset most recent commit )

To delete repository remotely :
Deleting a GitHub repository directly through Git commands on your local machine isn’t possible.
But we can do this by sending delete request to git API —

curl -X DELETE -u "<username>:<your_access_token>" < Repo URL>

These are only some of the commands that I use regularly

--

--

Akarsh Sharat
0 Followers

Tech enthusiast | Tired of endless document searches... Documenting my tech journeys to simplify my tech dilemmas.