January 16, 2023

Git workflow

Preface

Git workflow is divided into two situations.


New Repo

  1. Setup credentials (email & username) in Git. Tell git who is the author of the code.

    Git uses a username to associate commits with an identity. The Git username is not the same as your GitHub username (of course you can make it the same).

    The name you set will be visible in any future commits you push to GitHub from the command line. If you want to keep your real name private, use any text as your Git username.

    Changing the name associated with your Git commits using git config will only affect future commits and will not change the name used for past commits.

    The error happens when I try to commit without setup credentials.

    Error Message

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    Author identity unknown

    *** Please tell me who you are.

    Run

    git config --global user.email "[email protected]"
    git config --global user.name "Your Name"

    to set your account's default identity.
    Omit --global to set the identity only in this repository.

    fatal: unable to auto-detect email address (got 'andy@Andy-iMacPro.(none)')

    This is because we did not specify the code’s author or who is currently operating this commit.

    This can be solved by setting up your credentials (email and username).

    Solution

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # --global: Setting your Git email and username for every repository on your computer
    # If you want to setup locally. Eg. Only current repo, just remove --global

    # Set a Git email syntax
    git config --global user.email "[email protected] Your-Email-Address"
    # Eg.
    git config --global user.email "[email protected]"

    # Set a Git username syntax
    git config --global user.name "Your Name or Author Name"
    # Eg.
    git config --global user.name "Andy"

    If you do not want to set for every git repo in your computer, just remove --global so it can set up credentials for a single repo.

    Validation

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # Validate current git user email address
    git config --global user.email
    # Sample output
    [email protected]

    # Validate current git username
    git config --global user.name
    # Sample output
    Andy
  2. Initialise an empty git repository

    1
    2
    3
    # Eg. TestRepo
    cd /Andy/Repo/TestRepo
    git init
  3. Write code or make any changes

  4. Add files for commit

    1
    2
    3
    4
    5
    # Add all files in current directory
    git add .

    # Or add a specific file in the current directory for commit. Eg. README.md
    git add README.md
  5. Commit with a commit message

    1
    git commit -m "Commit message related to your current commit"
  6. Create a branch called “main”

    1
    git branch -M main
  7. Add remote git repository endpoint

    1
    git remote add origin https://github.com/JinchuanL/TestRepo.git	
  8. Push to the remote repository

    1
    git push -u origin main

Exist repo for development

1
2
3
4
5
6
7
8
9
# Get the latest update first
git pull

# Write your code

# Commit your code to the remote repository
git add .
git commit -m "Commit message"
git push -u origin main


Reference

About this Post

This post is written by Andy, licensed under CC BY-NC 4.0.

#Git