Git

Basics

  • git setup

git config --global user.name "NafiAsib"
git config --global user.email "[email protected]"
git config --global color.ui true # enable colored output in terminal
git config --global core.editor nvim

# ↓ OLD
# git config --global credential.helper cache --timeout=3600 # cache password for 3600 second
# git config --global credential.helper cache
# git config --global --unset credential.helper
# git config --global core.editor code      # only in windows
# git config --global core.autocrlf true    # only in windows
  • SSH key

cd ~/.ssh
ssh-keygen -t rsa -C "[email protected]" -f "id_rsa_github"
# -t rsa ⇒ Specifies the type of key to create. In this case, it's rsa
# -C ⇒ comment
# -f ⇒ filename

Now copy the content of id_rsa_github.pub and set as SSH key in GitHub.

You can confirm that SSH is okay by typing ssh -T [email protected]

If you see following message, you're done!

Hi NafiAsib! You've successfully authenticated, but Github does
not provide shell access.
  • git directory initialization

git init
git add .
git commit -m "commit message"
git branch -M main # change default branch to main from master
git remote add origin repo-link # https link of remote directory
git push -u origin main
  • remove a file

git rm file-name # from both git and locally
git rm --cached file-name # from only git
git rm -r directory-name # directory
# commit and push

reset last commit

$ git reset --soft HEAD~1

git merge conflicts

To solve merge conflict between two branch, first pull the other branch in your working branch. Then merge all conflict and push. It's better to squash your commits to make the commit tree clean.

$ git pull dev # dev is the already pushed branch
$ git checkout feature_branch # working branch
$ git pull origin dev
# fix all merge conflict
$ git add .
$ git commit -m 'merged message'
$ git push

git squash last X commits

$ git reset --soft HEAD~X     # last X commits
$ git commit -m "new commit message"

Reset rebase

$ git reflog
$ git reset --hard HEAD@{5}

git ammend

$ git commit --amend # prompt editor to change most recent Git commit message
$ git commit --amend -m "an updated commit message" # doesn't prompt editor

using interactive rebase

$ git rebase -i HEAD~5
# replace pick to squash to commits you want to squash

Errors & fix

Error
fatal: unable to access 'https://github.com/sitndeal/sitndeal.git/': Could
not resolve host: github.com

Solve

$ git config --global --unset http.proxy
$ git config --global --unset https.proxy

restart terminal

$ git remote -v
> origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
> origin  https://github.com/USERNAME/REPOSITORY.git (push)

$ git remote set-url origin [email protected]:USERNAME/REPOSITORY.git

$ git remote -v
# Verify new remote URL
> origin  [email protected]:USERNAME/REPOSITORY.git (fetch)
> origin  [email protected]:USERNAME/REPOSITORY.git (push)

Resources

Last updated