Link Search Menu Expand Document

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)

Git-Image

Table of contents

Git basic command

    git init
    git add [file-name]
    git status
    git commit
    git commit -m "..."
        # when committing changes, -m = message
        # whatever you have in " ... " is the commit message
    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

Full Github tutorial

  1. Check remote:

     git remote -v 
         #outputs: 
         #origin ssh://[email protected]:USER/repo-name.git (fetch)
         #origin ssh://[email protected]:USER/repo-name.git (push)
    
  2. Switch SSH to HTTPS:

     git remote set-url origin https://github.com/USER/repo-name.git
    
  3. If directory is not set remote

     git remote add origin ssh://[email protected]:USER/repo-name.git
     git remote -v 
         #see the path is set 
    
  4. Simple git init » git add [file]

     echo "hello world" > test1.txt
     git add test1.txt 
     git status
    

How to pull repository from remote to local machine

  • Create a directory on the local PC, then cd into it
  • git init will create .git file in the directory
  • Set remote address git remote add origin https://github.com/<USER>/<REPO>.git
  • Run git checkout branch-name to switch to the correct branch
  • Run git pull and all data will be pulled from github to local PC, use ls -a to view listed files
  • Run git branch -r to see list of available branches