View on GitHubs

Resources

Install Git

Installing on Windows

  • Install Git for Windows, which provides Git Bash (terminal emulator for running Git commands)
  • Or, install using Windows Powershell
winget install --id Git.Git -e --source winget

Installing on Linux

https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

  • Install Git
    sudo apt install git

Configure GitHub

  • Configure GitHub, replacing "Your name" and "your.email@example.com" with your info (including the quotes) to link your local Git profile with GitHub

    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
  • Verify configuration

    git config --get user.name
    git config --get user.email
    # List all settings
    git config --list

Basic Git Workflow Example

Creating a local repository

  • Create a local repository myTest and change the current working directory to myTest:

    mkdir myTest && cd myTest
  • Initialize a .git subdirectory in the current working directory

    • Contains a hidden .git folder with subdirectories for objects, refs, and template files
    • A HEAD file is also created which will point to the currently checked out commit
    git init
    • If successful, you’ll see a message like:
    Initialized empty Git repository in /home/user/Mytest/.git/
    
  • Check the current state of the working directory and the staging area.

    • Shows which changes have been staged, which haven’t, and which files aren’t being tracked by Git.
    git status
  • Create a README file to describe the repository:

    • We will use nano (sudo apt install nano), a simple, user-friendly text editor for the command line
    nano README.md
    # Add some text, example:
    This is a git repo
    # Save the file by pressing CTRL + X, then Y, then press Enter
  • Optional: Create a sample.c file:

    #include<stdio.h>
    int main()
    {
        printf("hello world");
        return 0;
    }

Staging Files in the Git Index

  • Create and add files to the index.

    git add README.md
    git add sample.c

Committing changes made to the index

  • Commit the changes with a message:

    git commit -m "Initial commit"

Creating a repository on GitHub

  • Create a repository on GitHub with the same name as your local repository (e.g., “Mytest”).

    • Then, connect your local repository to the GitHub repository (Replace 'user_name' with your GitHub username):
    git remote add origin https://github.com/user_name/Mytest.git

Pushing files from a local repository to a GitHub repository

  • Push the local repository contents to GitHub
    • The first time running this command, you need to link the local master branch to the master branch on remote repository (origin)
git push -u origin master
# Can also use `git push --set-upstream origin master`
 
# Note: Some repositories may use 'main' as the default branch instead of 'master'.
# In that case, use the following command:
# git push --set-upstream origin main
  • For subsequent pushes
git push origin master
 
# Or if your default branch is 'main':
# git push origin main
  • You may get a permission denied error when pushing to GitHub if SSH keys aren’t set properly (See Connecting to GitHub with SSH)

  • Enable colorful output with git

    git config --global color.ui auto