Ankit Kumar
Ankit Kumar Programmer, Technologist, Technical Writer, YouTuber and Tutor

Working with Github (or any software development version control using Git) for the beginners

Working with Github (or any software development version control using Git) for the beginners

Working with Github (or any software development version control using Git) for the beginners

Many new developers face this problem when they start working on a project which is shared among many team member and many of them need to work together on the project. It becomes hard for newbies to create PR and adding their work to the upstream project repo. I am trying to make this easier for all the newbies who are going to work on the project collaboration in the team. By following these steps, it becomes easier to handle changes, stashing, committing, creating, and merging PR after approval.

Let’s go through the steps with commands and sample project on Github. For getting hands-on experience of the below steps, feel free to fork this Github repo and follow along

  • Clone project from upstream to the local repo

    1
    
         git clone git@github.com:AnkitDroidGit/git-basics-for-newbies.git
    
  • Open project with your favorite IDE/Code Editor
  • Pull the latest changes from upstream if you haven’t pulled changes from quite some time (think of changes merged by other members of your team)

    1
    
         git pull
    
  • Make your changes on the master (default) branch. I am adding a new file called contributors.md and appending it with my name Ankit Kumar.
  • Add file(s) to git for tracking by git add

    1
    
         git add name
    

    You can add all files by running below command

    1
    
         git add .
    
  • Check differences by running the following command.

    1
    
         git diff
    
  • Now suppose someone from your teams has already added and merged his changes to the master branch. If you are going to commit and push your changes without synching repo with upstream, you might get conflicts that become hard to resolve later in case of many conflicts. Its a great idea to sync your local repo with upstream. We will do it here by the below commands.

    • First, stash your changes. Stashing saves your working directory and index state WIP on master: with the latest commit

      1
      
          git stash
      
    • Run git pull for latest changes from upstream branch, if any.

      1
      
          git pull
      
    • Apply your stashed change to an active changelist or default changelist.

      1
      
          git stash apply
      
  • Check your difference again by running git diff

    1
    
        git diff
    
  • Resolve conflicts if any. Resolving conflicts now is easier compared to the time of many many conflicts at the time of creating PR without synching.
  • Now, add commit message

    1
    
        git commit - m "added my name in contributors.md"
    
  • Checkout to a new branch

    1
    
        git checkout -b addedName
    
  • Push your changes to the newly checked out branch. It will create a new branch on upstream with the same name your have checked out with.

    1
    
        git push --set-upstream origin addedName
    
  • Visit Github (or any software development version control using Git) repo on the browser. Click on compare and pull request, Write your PR name with appropriate comments. Add reviewers to request team members to review your PR.

  • Finally click on create pull request You have done your work already. Wait for the reviewer to review

  • Once PR is approved by the reviewer(s). Visit your branch and merge your branch.

  • On your local repo, pull master again before making any other changes, follow the steps again.

Also, to be notified about my new articles and stories, follow me on Medium, Github. You can find me on LinkedIn as well. Cheers!