Tag Archives: GitHub

How to Use Git

Motivation:

You want to use Git to version your files or share your files with other people.

Solution:
  • Register a GitHub or a GitLab account.
  • Create a GitHub repository or a GitLab project.
  • Download and install a Git client.
  • Generate a personal access token.
  • Pull (checkout) a remote repository (e.g. https://github.com/huybien/asp.net-core.git) to an empty local folder (e.g. C:\Users\admin\Downloads\code):
    git init
    git config user.email "[email protected]"
    git config user.email
    git config user.name "Huy Bien"
    git config user.name
    git config credential.helper ""
    cd C:\Users\admin\Downloads\code
    git remote add origin -f https://github.com/huybien/asp.net-core.git
    git pull origin main
    / * or * /
    git pull origin master
    / * Enter username and token. */
  • Pull (checkout) a remote repository (e.g. https://github.com/huybien/asp.net-core.git) to a local folder that contains existing code (e.g. C:\Users\admin\Downloads\code):
    cd  C:\Users\admin\Downloads\code
    git init --initial-branch=main
    git config user.email "[email protected]"
    git config user.email
    git config user.name "Huy Bien"
    git config user.name
    git config credential.helper ""
    git remote add origin https://github.com/huybien/asp.net-core.git
    git fetch --all
    / * Enter username and token. */
    git add *.*
    git commit -m "new files added"
    git push -u origin main
    / * Enter username and token. */
  • Push local files to a remote empty repository:
    git init
    git config user.email "[email protected]"
    git config user.email
    git config user.name "Huy Bien"
    git config user.name
    # Tell Git to use the Git Credential Manager as its credential helper, which is a tool that securely stores and retrieves credentials (such as usernames, passwords, and personal access tokens) when interacting with remote repositories over HTTPS.
    git config credential.helper manager
    git config credential.username huybien
    git config --global credential.useHttpPath true
    git config --global credential.helper manager
    git add *.*
    git commit -m "first commit"
    git branch -M main
    git remote add origin https://github.com/huybien/asp.net-core.git
    git push -u origin main
  • Push changes to a remote repository:
    git config user.email "[email protected]"
    git config user.email
    git add *.*
    git commit -m "CP form"
    git branch -M main
    git push -u origin main
  • Update (fetch and merge) a local repository:
    git pull origin main
    /* or */
    git branch --set-upstream-to=origin/main main
    git pull
  • Force updating (fetch and overwrite) the current repository:
    git fetch --all
    git reset --hard origin/main
    git clean -fd git pull
  • Force updating (fetch and overwrite) a local repository (e.g. C:\Users\admin\Downloads\code):
    git -C C:\Users\admin\Downloads\code fetch --all 
    git -C C:\Users\admin\Downloads\code reset --hard origin/main 
    git -C C:\Users\admin\Downloads\code clean -fd
    git -C C:\Users\admin\Downloads\code pull
  • Reset (Revert) a local repository to a previous version:
    cd C:\Users\admin\Downloads\code
    git log --oneline
    git reset --hard 4355842
    // where 4355842 is a version id.
  • Remove all cached files:
    git rm -r --cached .
  • Display remote URL:
    git config --get remote.origin.url
  • Discard local changes of file:
    git restore "Features/01_How-to Guides.png"
  • Update GitHub personal access token on Windows:
    # Go to Control Panel, open Credential Manager, click Windows Credentials tab, and remove the related stored Git account if necessary.
    # Tell Git to remember your personal access token so that you don't have to re-enter them every time you push, pull, or fetch from a remote repository.
    git config --global credential.helper manager
    # Tell Git to key credentials by full path (host + owner + repo).
    git config --global credential.useHttpPath true
    # Set explicit username per repo (helps the helper pick the right entry).
    git config credential.username your-personal-username
    # First pull will ask for credentials
    git pull
    # After entering the token, it will be saved for future use.

     

     

     

     

    Topic 11 – Software Construction

    Why do I need to learn about software construction?

    Knowing how to write code does not mean that you know how to create real-world software.

    In real world projects, you will need to know how to manage your code, how to read the existing code, how to write code following standard styles, how to ensure that your code is working, how to automate your the process of building, testing and deploying your code, how to handle errors in your application, how to optimize your code for better speed, how to write secure code, how to avoid code duplication, how to create readable code, how to create code faster.

    That’s why you need to learn about software construction.

    What can I do after finishing learning software construction?

    You will know how to create real world software with a team.

    Hmm! Is it really useful?

    If you doubt its usefulness then you can delay learning it until you are tasked to create a software system and you complete a half of it and are stuck there because when you add one more feature you will get tons of bugs due to the new code, or when you finish fixing 1 bug, you get 3 other bugs due to the code modification.

    An other scenario is when it takes another person 2 weeks to read 1000 lines of the code that you wrote in order to fix a bug or to add a new feature because your code is unstructured.

    Another scenario is when you are asked to improve some existing code for better performance or to refactor some code for clarity before adding a new feature but you do not know how to accomplish the task.

    Alright! What should I do now?

    First, please read this book to get familar with the core software construction activities: Steve McConnell (2004). Code Complete. Microsoft Press.

    After that, please read this book to gain hands-on experience with using Git for managing source code versions: Jon Loeliger and Matthew McCullough (2012). Version Control with Git: Powerful Tools and Techniques for Collaborative Software Development. O’Reilly Media.

    Alternatively, you can read this book to gain hands-on experience with using Subversion for managing source code versions: Ben Collins-Sussman et al. (2011). Version Control with Subversion.

    After that, please read this book to learn about continuous code integration: Paul M. Duvall et al. (2007). Continuous Integration Improving Software Quality and Reducing Risk. Addison-Wesley.

    After that, please read this book to learn how to write readable, maintainable code: Robert C. Martin (2009). Clean Code: A Handbook of Agile Software Craftsmanship. Pearson Education.

    After that, please read the two books below to learn how to write and run unit tests:

    After that, After that, please read the two books below to learn how to convert existing unstructured, hard-to-test code into readable and maintainable code:

    If you have to work with legacy code, please read the two books below to learn how to read and maintain it in a structured way:

    After that, you will need to learn:

    If you are not familiar with .NET, please find equivalent books to complete the tasks using other technologies or frameworks.

    After that, please read the two books below to learn how to use Docker and Kubernetes or deploying and operating software:

    You can learn the basic concepts of Kubernetes with the book, and then try exploring the tool using the official documentation if you encounter any issues with the example code in the book.

    After that, please read this book to learn how to use Terraform to provision resources for deploying and operating software: Yevgeniy Brikman (2022). Terraform – Up and Running. O’Reilly Media.

    Terminology Review:

    • Version Control.
    • Coding Standards.
    • Unit Tests.
    • Continuous Integration.
    • Refactoring.
    • Legacy Code.
    • Code Reading.
    • Data Providers.
    • Object/Relational Mapping (ORM).
    • Reporting Services.
    • Authentication.
    • Authorization.
    • OAuth 2.
    • Proof Key for Code Exchange (PKCE).
    • JSON Web Token (JWT).
    • OpenID Connect (OIDC).
    • The Backend for Frontend Pattern.
    • Performance.
    • Containers.
    • Container Images.
    • Container Image Layers.
    • Docker.
    • Docker CLI.
    • Dockerfiles.
    • Docker Volumes.
    • Bind Mounts.
    • Docker Compose.
    • Container Orchestrator.
    • Swarm.
    • Kubernetes.
    • Deployments.
    • ReplicaSets.
    • Pods.
    • Deployment Manifest.
    • ClusterIP Services.
    • LoadBalancer Services.
    • NodePort Services.
    • ExternalName Services.
    • headless Services.
    • Namespaces.

    After finishing software construction, please click on Topic 12 – Software Testing to continue.