Git and Github Tutorial

This article will get you familiarize with one of the most popular Version Control System (VCS) Git. Download

    Following are the main steps that you have to always follow.
  1. Open the directory in terminal and Initialize the empty git repository.
    git init

    It will create a .git directory in the current directory.
  2. Now one of the useful command here is
    git status
    . It will tell you about what files are modified, added or deleted.
  3. Now it is time to add the files to staging area (Tracking changes). for that we'll use the command :
    git add (filename)
    for specific file or
    git add .
    for adding all files
  4. Once the files are in staging area you can save them to commit history via command
    git commit -m "My First Commit"
    and you can check all your commit from the command
    git log
    or
    git log --oneline
  5. Now our commit is saved we can add multiple commits in this repository.
  6. Let's upload these changes to GitHub.
    for that we have to link our local repository to github repository.
    git remote add origin URL
    in my case the URL is "https://github.com/OmerBhatti/git-tutorial.git"
  7. To Push your code to linked repository you just have to type
    git push -u origin master / branch-name
  8. Now our local repository is synchronized with online repository what if we edit files on our online repository
    and want the changes in our local repository? For that we'll use
    git pull
    command. if we try pushing somethiong without
    git pull
    it will raise a error like this To resolve the issue you have to type
    git pull
    it will fetch all the changed files to your local repository and now your both repositories are synchronized. If files have conflict on same line it may ask you what changes you want in your latest version.
  9. Branching

  10. One of the main use of git is for testing new features without Disrupting the stable version for that we create new branch with the command
    git checkout -b new-feature
    this command will create new branch and move the HEAD to this branch. Every branch created is the copy of the branch it was created from.
  11. Now i can make any changes and commits in my new branch without disturbing code in master/main branch. When i am done with testing the features i can merge the new-branch with the master/main branch. to merge new branch in your main branch:
    First switch to main branch by
    git checkout master

    Then i will type
    git merge new-feature
    to merge new-feature into master branch.
    Always remeber to checkout into the branch in which you want to merge another branch.
  12. Now i have merged the branch new-feature into my master branch i can delete it using this command
    First switch to main branch by
    git branch -d new-feature
  13. Moving between Commits

  14. To switch to another commit just type this command
    git checkout commit-hash

    You can get the hash of commit by doing command
    git log
    or
    git log --oneline
    for short preview At this all the directory sturcture will be like it was on your that particular commit
    To go back to your latest code just type
    git chackout master