Skip to content

Instantly share code, notes, and snippets.

@ovntatar
Last active March 13, 2023 08:21
Show Gist options
  • Save ovntatar/c42da4bfcbd3edcfd9997d45754cfd69 to your computer and use it in GitHub Desktop.
Save ovntatar/c42da4bfcbd3edcfd9997d45754cfd69 to your computer and use it in GitHub Desktop.
git overview
### How to write a Git commit message
https://chris.beams.io/posts/git-commit/
1. Separate subject from body with a blank line
2. Limit the subject line to 50 characters
3. Capitalize the subject line
4. Do not end the subject line with a period
5. Use the imperative mood in the subject line
6. Wrap the body at 72 characters
7. Use the body to explain what and why vs. how
### Make my work easier and set SSH key for repo
1 generate a ssh key
ssh-keygen -t ed25519 -C "<EMAIl>"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
xclip -selection clipboard < ~/.ssh/id_ed25519.pub
2 add key to github.com repo dashboard
3 config cli and set git remote repo URL
git config core.sshCommand "ssh -i ~/.ssh/id_ed25519 -F /dev/null"
vi ~/.ssh/config
Host github.com
User git
Hostname github.com
IdentityFile ~/.ssh/id_ed25519
git remote set-url origin git@github.com:<USER>/<REPO>.git
### BASICS ####
git init: Initialize a Git repository in the current folder
git status: Show the current status of your Git repository (the “working tree” status)
git add .: Track changes of all files in your Repository
git commit -m "your message": Save updated code to a new Commit named “your message”
git log: List all Commits inside your Branch
git checkout branch-name: Jump to the last Commit of a Branch
git checkout commitid Jump to a specific Commit of a Branch (commitid should be the ID of the Commit you want to checkout)
git checkout -- .: Jump back to the last Commit and remove any untracked changes
get reset --hard: Turn the selected Commit into the new HEAD --- --soft | --mixed (default ) --hard |
git branch: List all Branches inside your Repository
git checkout -b branch-name: Create a new Branch named branch-name
git merge branch-name: Merge two Branches, branch-name is the Branch you merge with the Branch you’re currently working in
git merge my-chnages # will merge chnages from my-chnages into master
git branch -D branch-name: Delete the Branch named branch-name
#### Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits #####
# Revert back the code that was written
git checkout <FILE> -- delete all chnages on a file and turn back to commited version
check that with git status and git diff
# Updating the commit message alone (amend)
modily commi message witout another commit
git commit --amend -m "new commit message"
#Accidentally left off a file that we wanted to commit
git commit --amend
#Move the commits to a different branch (cherry-pick)
git cherry-pick <commit id from master>
3 different types of reset
git reset --soft <commit id> # would not delete old commits -- mix chnages in the staging area witjout commits -- hard will delete all commits till <commit id>
# Discard all local changes in a file ( Please be careful: discarding local changes cannot be undone! )
git restore <file>
# Restoring deleted files
git restore <file>
# Discard chunks / lines in a file
git restore -p <file>
# Discarding all local changes
git restore .
# Reverting a commit in the middle
git revert <comnmit id>
# Resetting a file to an old revision
git restore --source <file>
# Getting rid of untracked files (git clean)
git clean -df
# Retrieve critical files that were lost, and you want them back (git reflog)
git checkout <commit id>
# Backing out changes, when other people have already checked out your changes (git revert)
## Generate and aply patch ###
git diff > mypatch.patch
But sometimes it happens that part of the stuff you're doing are new files that are untracked and won't be in your git diff output. So, one way to do a patch is to stage everything for a new commit (git add each file, or just git add .) but don't do the commit, and then:
git diff --cached > mypatch.patch
Add the 'binary' option if you want to add binary files to the patch (e.g. mp3 files):
git diff --cached --binary > mypatch.patch
You can later apply the patch:
git apply mypatch.patch
### OR for multiple files and directories
git format-patch main --stdout > f.patch
git apply --stat f.patch
@ovntatar
Copy link
Author

git stash (To save your un-committed changes in a "stash". Note: this removes changes from working tree!)

git stash apply (to apply stash to working tree in current branch)

git stash apply stash@{12} (if you will have many stashes you can choose what stash will apply -- in this case we apply stash 12)

git stash drop stash@{0} (to remove from stash list -- in this case stash 0)

git stash pop stash@{1} (to apply selected stash and drop it from stash list)

@ovntatar
Copy link
Author

git revert vs reset Reverting undoes a commit by creating a new commit. ... Contrast this with git reset , which does alter the existing commit history. For this reason, git revert should be used to undo changes on a public branch, and git reset should be reserved for undoing changes on a private branch.

The basic purpose of git rebase and git merge is the same, i.e. they help us to bring changes from one branch into another. The difference is that git rebase re-writes the commit history:

@ovntatar
Copy link
Author

@ovntatar
Copy link
Author

How do I use 'git reset --hard HEAD' to revert to a previous commit?

@ovntatar
Copy link
Author

ovntatar commented Oct 15, 2021

How to I use 'git reset --hard HEAD' to revert to a previous commit?

git reset --hard f414f31
git clean -df

or


$ git reset --hard HEAD       (going back to HEAD)
$ git reset --hard HEAD^      (going back to the commit before HEAD)
$ git reset --hard HEAD~1     (equivalent to "^")
$ git reset --hard HEAD~2     (going back two commits before HEAD)

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