Skip to content

Instantly share code, notes, and snippets.

@sekedus
Last active November 15, 2024 05:21
Show Gist options
  • Save sekedus/f9875a4e7113b24d192f11a7be8637f6 to your computer and use it in GitHub Desktop.
Save sekedus/f9875a4e7113b24d192f11a7be8637f6 to your computer and use it in GitHub Desktop.
Git tutorial for beginners

Git tutorial for beginners

Create a new repo on github
  1. Login to github: gh auth login
  2. Set global options (name & email):
git config --global user.name "<Your Name>"
git config --global user.email "<[email protected]>"
  1. Create new repo & clone: gh repo create
  2. Initialize a git repository: git init
  3. Set default branch name: git branch -M main
  4. Add a remote repository:
git remote add origin https://github.com/<your_username>/<your-repo>.git

Upload local content to a remote repository
  1. Include updates
git add .
  1. Commit changes
git commit -m "<commit message>"
  1. Upload to remote repository
git push

# first commit use:
git push -u origin main

Overwrite a commit after pushed to remote

source

  1. Include updates
git add .
  1. Modify old commit
git commit --amend -m "<commit message>"
  1. Overwrite history on the Github remote
git push -f

Discard commit

source

  • Delete the most recent local commit, without discard local files changes:
git reset --soft HEAD~1
  • Delete commits and discard/delete all changes in your working tree (local files & untracked files or directories:
git reset --hard origin/<branch>

# or

git reset --hard HEAD~5 # reset current branch to 5 commits ago

# or

git reset --hard <commit_sha_hash>

Comparing/diff commits

source

Compares two arbitrary commits, using commit SHA-1 hash.

Example:

https://github.com/sekedus/tamp/compare/3184ad9db2c6de28794a2ee1bab91fb805f9d826..b431a20697524f9d3a332b916748457934fdc710

or use the shortened SHA code

https://github.com/sekedus/tamp/compare/3184ad9..b431a20
Delete git tag local and remote

source

# delete local tag '12345'
git tag -d 12345
  
# delete remote tag '12345' (eg, GitHub version too)
git push origin :refs/tags/12345
  
# alternative approach
git push --delete origin tagName
git tag -d tagName
Changing a GitHub repository's visibility

GitHub Docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment