Skip to content

Latest commit

 

History

History
304 lines (221 loc) · 9.86 KB

use-git.md

File metadata and controls

304 lines (221 loc) · 9.86 KB

Basic git operations on the command line

Clone a Repository

To check-out a repository that you will be committing code to, it is best to clone it. A repository can be cloned into the current directory with the following command.

git clone [email protected]:Senzing/knowledge-base.git

See Current Status of Cloned Repository

This will list pending changelist as well as added/changed/deleted items

git status

Update Current Branch

git pull

Add an Item to the Pending Changelist

git add <filename>

See the Diff of Pending Changelist

git diff

Commit the Pending Changelist

Note: You want to make sure your repository is synced to github before commiting your changelist to avoid a merge

git commit

This will open a text editor for you to comment the changelist

You should now push this committed change to github so others can pull it

git push

See Local Branches in the Repository

This will show the branches of the repository you have checked out, and which branch is currently active locally

git branch

See All Remote Branches in the Repository

This will show all the remote branches available for checkout first make sure you local repository is in sync with the origin:

git fetch origin

then list branches:

git branch -v -a

Switch branches

git checkout <branch_name>

You can also use

git switch <branch_name>

Create a new remote branch

First create the branch locally, then push it to github

git checkout -b <branch_name>
git push origin <branch_name>
git branch --set-upstream-to=origin/<branch_name> <branch_name>

Merge changes in main to current branch

In repositories created after October 2020, the "main" branch is used.

GIT_CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
git checkout main
git pull
git checkout ${GIT_CURRENT_BRANCH}
git merge origin/main
git push

Merge changes in main to current branch

In repositories created before October 2020, the "master" branch is used. However, they've since been migrated to "main" in May 2022.

GIT_CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
git checkout main
git pull
git checkout ${GIT_CURRENT_BRANCH}
git merge origin/main
git push

Delete a remote branch

first delete it locally

git branch -d <branch_name>

then delete it on github

git push origin --delete <branch_name>

Help! I tried to commit to main, now what?

No problem, we got you. Make sure you are on the branch to which you have been committing. git log to check how many commits you want to roll back. undo the commits with git reset HEAD~N where “N” is the number of commits you want to undo.

eg, to undo one commit:

git reset HEAD~1

now create your branch, switch to it, and continue on as if that didn't happen. Do be sure to verify that you're only committing the changes you want with git status

One time configuration for your git command line

set up globals

Set up your name and email

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

Prune deleted branches from your local when you pull

git config --global fetch.prune true

Block command line pushes that expose your personal email address

  1. Change the GitHub settings to keep your email address private
  2. Set your email address in Git to the noreply email address generated by GitHub
  3. Test pushing a commit to valiate settings changes.

Sign commits locally

Prerequisite: Install GnuPG

NOTE: The following steps have been tested on OSX and Linux.
If you encounter issues please refer to the respective GitHub documentation linked in the Additional Resources section below.

  1. Generate a new GPG key.

    1. Open Terminal / Git Bash.

    2. Generate a GPG key pair.

      gpg --full-generate-key
      1. When prompted input the number corresponding to the kind of key and press Enter.
        • Recommended input: ECC (sign and encrypt).
          • If using ECC, input the number corresponding to the elliptic curve and press Enter.
          • Recommended input: Curve 25519.
        • If ECC is unavailable you can use RSA and RSA.
          • Recommended key size: 4096
      2. Input the length of time the key should be valid and press Enter.
        • Recommended input: 1y.
      3. Verify your selections are correct.
      4. Input your name and press Enter.
      5. Input your email address and press Enter.
      6. Optionally input a comment and press Enter.
      7. Verify your selections, input O and press Enter.
      8. Type a secure passphrase.
        • NOTE: You will be prompted for this passphrase when running git commit
          The default cache for this passphrase is typically 2 hours.
          See links in additional details for changing default caching values.
    3. List the long form of the GPG keys.

      gpg --list-secret-keys --keyid-format=long
    4. From the list of GPG keys, copy the long form of the GPG key ID you'd like to use.
      In this example, the GPG key ID is 3AA5C34371567BD2:

      $ gpg --list-secret-keys --keyid-format=long
      /Users/hubot/.gnupg/secring.gpg
      ------------------------------------
      sec   4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
      uid                          Hubot <[email protected]>
      ssb   4096R/4BB6D45482678BE3 2016-03-10
  2. Configure Git to use the key generated in the previous step.

    1. Open Terminal / Git Bash.

    2. Unset any existing git signing key format.

      git config --global --unset gpg.format
    3. Use the long form of the GPG key ID from Step 1.iv above to set your primary GPG signing key in Git.
      In this example, the GPG key ID is 3AA5C34371567BD2:

      git config --global user.signingkey 3AA5C34371567BD2
    4. Configure Git to sign all commits by default.

      git config --global commit.gpgsign true
  3. Add the GPG key to GitHub.

    1. Open Terminal / Git Bash.

    2. Use the long form of the GPG key ID from Step 1.iv above to export your public GPG key.

      • Paste the text below, substituting in the GPG key ID you'd like to use.
        In this example, the GPG key ID is 3AA5C34371567BD2:

         gpg --armor --export 3AA5C34371567BD2
         # Prints the GPG key ID, in ASCII armor format
    3. Copy your GPG key, beginning with -----BEGIN PGP PUBLIC KEY BLOCK----- and ending with -----END PGP PUBLIC KEY BLOCK-----.

    4. Login to the GitHub UI.

    5. In the upper-right corner of any page, click your profile photo, then click Settings.

    6. In the "Access" section of the sidebar, click SSH and GPG keys.

    7. Next to the "GPG keys" header, click New GPG key.

    8. In the Title field, type a name for your GPG key.

    9. In the Key field, paste the GPG key you copied in Step 3.iii above.

    10. Click Add GPG key.

  4. Checking your commit and tag signature verification status.

  5. Optional: Use gpg-agent flags for configuring default caching for authentication or preset passphrase.

    • See links in Additional Resources below.

Troubleshooting

Error:

gpg: signing failed: No such file or directory

fatal: failed to write commit object

Ensure GPG_TTY is configured in the environment. See Configure TTY for more details.

Additional Resources