Skip to content

Start to understand the relations between git and github. Basic commands of git.

Notifications You must be signed in to change notification settings

yursds/Learn-Git-Commands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 

Repository files navigation

Git Commands Guide

This guide explains the main Git commands.

Table of Contents

  1. Setting Up Git
  2. Creating a Repository
  3. Checking the Status
  4. Adding Files
  5. Deleting Files
  6. Committing Changes
  7. Creating a New Branch
  8. Link to a Remote Repository (GitHub)
  9. Pushing Changes
  10. Example of First Initialization of (remote) Repository

Setting Up Git

Before you start using Git, you need to configure your username and email:

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

This sets your identity for all Git repositories on your system. You can omit the --global flag if you want to configure only the current repository.

Creating a Repository

To create a new Git repository:

git init

This initializes a new Git repository in your current directory.

Checking the Status

To check the current status of your repository:

git status

This shows the status of your working directory and staging area.

Adding Files

# To add a new file to your repository
git add <file-name>
# To add multiple files to your repository
git add <file-name-a> <file-name-b>

To add all files in the current directory:

git add .

REMEMBER to commit the changes!

Deleting Files

To delete a file from your working directory and index:

git rm <file-name>

REMEMBER to commit the changes!

Committing Changes

To save your changes to the repository:

git commit -m "Meaningful message that describes the changes you made"

Creating a New Branch

To create and switch to a new branch:

git checkout -b <branch-name>

This creates a new branch and switches to it.

Link to a Remote Repository (GitHub)

Create a repository on GitHub and link it to your local repository:

git remote add origin https://github.com/<Your-repository-name>.git

Pushing Changes

To push a branch to the remote repository:

git push -u origin <branch-name>

This command explicitly specifies that you want to push a particular branch (<branch-name>) to the remote repository named origin. Default name of remote repository on GitHub is origin.

The -u flag in git push -u origin main stands for --set-upstream and is used to set the remote branch as the upstream branch for the local branch. This means the local branch will be linked to the specified remote branch, allowing you to use shortened commands in the future. For example:

git push 
git pull


Example of First Initialization of (remote) Repository

# Create and initialize the .git folder
git init
# Add all files of the current folder to the stage
git add .
# Describe your commit with a message (example: "first commit")
git commit -m "first commit"
# Create main/master branch with name "main"
git branch -M main
# Link your local repository to your remote GitHub repository, already created on GitHub (example: "https://github.com/yursds/git_initRepo.git")
# Default name of remote repository on GitHub is origin 
git remote add origin https://github.com/yursds/git_initRepo.git
# Push "origin" to "main"
git push -u origin main

About

Start to understand the relations between git and github. Basic commands of git.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published