Git is a version control system used to track changes in files and manage project code. It helps developers save their work and upload code to platforms like GitLab and GitHub.

Step 1: Check Git Status

git status 

Purpose: Checks the current status of your project.

It shows:

  • Modified files
  • New untracked files
  • Staged files
  • Current branch name

Step 2: Add Files to Staging Area

git add. 

Purpose: Adds all modified and new files to the staging area.

The . represents all files and folders in the current project.

Step 3: Remove a File from the Staging Area

git restore --staged resume.html 

Purpose: Removes resume.html from the staging area without deleting the actual file or its changes.

Step 4: Commit Changes

git commit -m "to learn git" 

Purpose: Saves the staged changes in the local Git repository.

  • commit – Saves changes.
  • -m – Adds a message describing the changes.
  • "to learn git" – Commit message.

Step 5: Configure Git Username and Email

If Git asks you to configure your identity, execute:

git config --global user.name "Your Name" git config --global user.email "your-email@gmail.com" 

Purpose: Sets your username and email address globally for Git commits.

To check the configured details:

git config --global user_email git config --global user_name 

After configuring, commit your changes:

git commit -m "to learn git" 

Step 6: View Commit History

git log 

Purpose: Displays the commit history of the repository.

It shows:

  • Commit ID (SHA)
  • Author name
  • Date and time
  • Commit message

Step 7: Check File Differences

git diff 

Purpose: Displays the differences between your modified files and the last committed version.

  • Lines starting with - indicate removed content.
  • Lines starting with + indicate added content.

Step 8: Push Code to GitLab

git push 

Purpose: Uploads your committed changes from the local repository to the remote GitLab repository.
If this is the first push to a branch, use:

git push -u origin main 
  1. origin – Remote repository name.
  2. main – Branch name.
  3. -u – Sets the upstream branch for future pushes.

Step 9: Fix Access Denied / Authentication Error

Sometimes, when executing git push, you may get errors such as:
if getting error like access denied

  1. Open your GitLab account.
  2. Click your profile picture.
  3. Select Edit profile / Preferences.
  4. Open Access → Personal Access Tokens.
  5. Click Add new token.
  6. Enter a token name.
  7. Select an expiry date.
  8. Select the required permission:
    • write_repository
    • read_repository
  9. Generate the token.
  10. Copy and save the token securely. Important Never share your personal access token with anyone.
git remote set-url origin https://<userName>:<token>@<project path> 

Step 11: Push Using Personal Access Token

git push