Thursday, February 11, 2021

git - survival commands - become zero to hiro - git in 5 minutes

 git survival commands - zero to hiro

Terms to understand
Working Directory
 - The directory where you will be creating, modifying files or directories.
Staging area:
 - The location where files are ready to be saved (Committed). First files are stored in staging area and then we commit.
Repo:
  - Files/direcotries that are saved/committed. It can be local or remote repo.
  - Local repo is your local PC, and remove it like github, gitlab.

OK, Lets get started ...

1. Open account on github

2. download git bash or MobaXterm and install it
   # yum install git
   $ git version

3. Open your program and initilize git
   $ mkdir gitws; cd gitws
   $ git init
   .git dir is created which contains config info

4. Add your identity so that you can download/upload your files
   $ git config --global user.name "Sam"
   $ git config --global user.email "sam@linuxtab.com"

5. Add your remote repo
   $ git remote add origin <URL-TO-GitHub>

6. Pull the content or update your local repo with remote to sync
   $ git pull

7. To select or to go to a particular branch
   $ git checkout <branch_name>
   $ git checkout ws_task2
   $ git checkout master    # you can switch between the branches

8. Create a new branch and switch to it to make changes to the contents
   $ git checkout -b <branch_name>
   $ git checkout -b <ws_task3>
   $ cat > index.html
     Welcome to the club !!!

4. Show the status if there is anything changed/modified at staging area.
   $ git status

5. Add changed contents to the staging area
   $ git add <file or dir>
   $ git add mydir
   $ git add index.html
   $ git add .     # add everything

6. Save any changes make to the file in your branch. This is commit area.
   $ git commit -m <Detail what is changed>
   $ git commit -m "initial changes are made for login module"
   $ git commit -m "My first commit" index.html
   $ git log index.html    # shows all version of file with commit id
   $ git help log  # get help on subcommand
   $ git show commitid    # shows the detail log
   $ git diff commit_id_1 commit_id_2    # show the difference in two versions
   $ git ls-files

7. Switch to your master (origin) branch
   $ git checkout master

8. Lets merge the branch ws_task3 with master
   - All the changes in ws_task3 will be added to master (fast forward merge)
   $ git merge ws_task3
   $ git branch -d ws_task3    # remove the branch you created

9. Push the change to your remote repo
   $ git push -u origin <branch name>

No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...