Git tutorial for beginners
Create a new repo on github
ㅤ- Login to github:
gh auth login
- Set global options (name & email):
git config --global user.name "<Your Name>"
git config --global user.email "<[email protected]>"
- Create new repo & clone:
gh repo create
- Initialize a git repository:
git init
- Set default branch name:
git branch -M main
- Add a remote repository:
git remote add origin https://github.com/<your_username>/<your-repo>.git
ㅤ
Upload local content to a remote repository
ㅤ- Include updates
git add .
- Commit changes
git commit -m "<commit message>"
- Upload to remote repository
git push
# first commit use:
git push -u origin main
ㅤ
Overwrite a commit after pushed to remote
ㅤ- Include updates
git add .
- Modify old commit
git commit --amend -m "<commit message>"
- Overwrite history on the Github remote
git push -f
ㅤ
Discard commit
ㅤ- 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
ㅤ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
ㅤ# 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