Basic Git
Git is to synchronise files between local machine and remote repository. I was quite confused in the beginning but eventually got the logic after trying again and again.
- Origin » Remote address (such as GitHub or Bitbucket)
- Local » Local PC (Local)
Table of contents
Git basic command
git init
git add [file-name]
git status
git commit
git checkout -b [new-branch-name]
#switch to new branch
git checkout [existing_branch]
#switch branch
git push [remote] [branch-name]
git pull
git clone
git branch
#to checkout which branch am I on
git branch -D [local-branch-name]
#delete the branch locally
git push origin -d [remote-branch-address]
#delete remote branch
git fetch origin
# to get updated from origin
Drop untracked change
Changes not staged for commit:
git status
# Changes not staged for commit: ...
git stash save --keep-index
# Saved working directory and index state WIP on master: ...
git stash drop
# Dropped refs/stash@{0}
git status
# nothing to commit, working tree clean
Detached HEAD
Seeing this [detached HEAD c253b43]
, run this:
git push https://github.com/USER/my-repo-name.git HEAD:master
Search & replace
:%s/search/replace/g
#example run: $ :%s/search-word/changed-word/g
#this is to search for the entire file
:8,10 s/search/replace/g
#this is to search within range of 8-10
Github setup
-
Check remote:
git remote -v #outputs: #origin ssh://git@github.com:USER/repo-name.git (fetch) #origin ssh://git@github.com:USER/repo-name.git (push)
-
Switch SSH to HTTPS:
git remote set-url origin https://github.com/USER/repo-name.git
-
If directory is not set remote
git remote add origin ssh://git@github.com:USER/repo-name.git git remote -v #see the path is set
-
Simple
git init
»git add [file]
echo "hello world" > test1.txt git add test1.txt git status