Friday, February 19, 2021

GIT - Knowledge Base

Git 2.17.1



Goals:

  • Have a list of useful Git commands, along with GitHub basic concepts.

Concepts:
  • GitHub
  • GitHub Commit/Push:
  • GitHub Pull Request:
  • GitHub Merge:
  • GitHub Squash:
    • Git Squash is a Git feature that allows a dev to simplify the Git tree by merging sequential commits into one another. Basically, you start by choosing a base commit and merging all changes from the next commits into this one. This essentially makes it the same as having all the changes you made in several commits in just one commit—the base commit.


Diagrams:





GIT Commands:

  • Show the Git software version:
    • git --version

  • Show the actual branch and modified files in a working directory, staged for your next commit:
    • git status

  • Check Your Current Git Configuration:
    • git config --global credential.helper
      • This command will show you the credential helper that Git is using. If you see "manager," it means Git is using a credential manager to store your GitHub token.

  • If you've stored GitHub credentials using the git config command and want to remove them:
    • List Your Git Configuration:
      • git config --list
        • This command will display all the Git configuration settings, including any credentials.
    • Locate the Credential Configuration in the output of the command above. The specific configuration may vary depending on how you've set up your Git credentials.
    • Remove the Credential Configuration:
      • git config --global --unset <config-name>
        • E.g.:  git config --global --unset credential.helper
    • Verify Removal:
      • git config --list
        • Make sure the configurations you removed are no longer present in the output.
        • After successfully removing the credentials, Git won't use the stored credentials for GitHub authentication, and you'll be prompted to enter your credentials when needed.
    • Windows OS:
      • Open the "Credentials Manager"
        • Click on "Windows Credentials" button
        • Delete the GitHub entries under Generic Credentials

  • Show the URL that it originally cloned a local Git repository from:
    • When Offline:
      • git config --get remote.origin.url
      • OR
      • git remote get-url origin
    • When Online and authenticated with Github:
      • git remote show origin

  • Check which user Git is using to commit your changes:
    • git config --list | grep user.name
    • git config --list | grep user.email

  • Change the username or email that Git is using:
    • git config --global user.name "Your Name"
    • git config --global user.email "your_email@example.com"

  • Show the commit ID:
    • git rev-parse HEAD
    • git rev-parse --short HEAD

  • Show commit logs:
    • git log

  • Show the current commit:
    • git log | head -n 1

  • Show the Difference between two commits:
    • git diff <first-branch-name-or-commit-id> <second-branch-name-or-commit-id> -- <filename>
    • git diff develop origin/master -- README.md

  • How to Revert a single file:
    • git checkout [commit-ID] -- path/to/file
    • git checkout [commit ID]~1 -- path/to/file

  • How to Revert a single commit:
    • git revert <commit-hash>
    • git revert HEAD~2

  • How to Restore working tree files:
    • git restore path/to/file
    • The command can also be used to restore the content in the index with --staged, or restore both the working tree and the index with --staged --worktree
      • To restore all files in the current directory
        • git restore .

    • List your branches. A (*) will appear next to the currently active branch:
      • git branch
      • git branch -a

    • Switch to another branch and check it out into your working directory:
      • git checkout <branch-name> [--force]

    • Fetch down all the branches from the Git remote:
      • git fetch [alias]

    • Stashing the changes:
      • The git stash takes uncommitted both staged and unstaged changes, saves them away for further use, and then returns them from your working copy. You can run the git status so you can see the dirty state. Then run git stash to stash the changes:
        • git stash
        • git stash list
          • stash@{0}: WIP on my-branch: 1234567 Refactoring some code
          • stash@{1}: WIP on my-branch: 1234567 Refactoring some code
      • Re-applying your stashed changes:
        • The git stash pop removes the changes from your stash and re-applies them to your working copy.
          • git stash pop
        • You can choose which stash to re-apply like this:
          • git stash pop stash@{1}
        • If you want to re-apply the changes and keep them in your stash:
          • git stash apply stash@{0}
        • Use git stash show to view a summary of a stash:
          • git stash show
          • git stash show stash@{0}
        • You can also use the -p or --patch options to see the full diff of a stash:
          • git stash show -p

    • Commit your changes:
      • git add .
      • git commit -m "commit message"


    • Pull the updated code:
      • git pull
      • If it's failing due to some file changes, you need to handle that. See the Stash information above.
        • git stash
      • If you want to ignore and lose all changes that you made to that local repository, then you can:
        • git fetch origin
        • git reset --hard
        • git clean -f
        • git pull

    • Making a Git push from a detached head:
      • git branch new-branch-name
      • git push -u origin new-branch-name

    • Create a new branch from a commit ID:
      • Navigate to the commit in question, and then click on the <> button next to the commit in your history. This will show the web interface for browsing that particular commits snapshot of the repository.
      • Click the down arrow on this button to show the dropdown. You can create a new branch simply by typing in the name of the new branch in the search field.
      • Click on the "Create branch: ..." link at the bottom of this dropdown, and a new branch should be created.

    • Git Reset vs Git Revert vs Git Restore
      • git-revert:  is about making a new commit that reverts the changes made by other commits.
      • git-restore:  is about restoring files in the working tree from either the index or another commit. This command does not update your branch. The command can also be used to restore files in the index from another commit.
      • git-reset:  is about updating your branch, moving the tip in order to add or remove commits from the branch. This operation changes the commit history. This command can also be used to restore the index, overlapping with git restore.

    • Retrieve an entire repository from a hosted location via URL:
      • git clone <REPO-URL>

    • Create, Commit and Push a Tag:
      • git tag <tag-name> -a
      • git push origin <tag-name>

    • Open a repository in browser using GitHub CLI:
      • gh repo view -w


    GitHub Workflow:

    A workflow run is made up of one or more jobs, which run in parallel by default. To run jobs sequentially, you can define dependencies on other jobs using the jobs.<job_id>.needs keyword.
    Each job runs in a runner environment specified by runs-on.
    You can run unlimited jobs as long as you are within the workflow usage limits.

    • Runners:
      • GitHub UI -> Your Organizations ->  <organization-name> -> Settings -> Actions -> Runners
      • Create a new Runner -> GitHub-hosted runner
        • Name: <our-runner-name> (e.g: ubuntu-22.04-4cores-16gb)
        • Image: Ubuntu
        • Ubuntu Version: 22.04
        • Size: 4-cores 16GB RAM ...
        • Auto-scaling - Maximum Job Concurrency: 2
        • Groups: <our-runner-group>
        • Networking - No public IP address



    Troubleshooting:
    • "remote: Repository not found":
      • Remove all the github.com credential details from the system.
        • For mac
          • Delete the github.com password from the Keychain Access.
        • For windows
          • Delete the credentials from Credential Manager.



    References: