Git and GitHub

Git and GitHub

Source code management

Source code management (SCM) is used to track modifications to a source code repository. SCM tracks a running history of changes to a code base and helps resolve conflicts when merging updates from multiple contributors. SCM is also synonymous with Version control.

Source code management has two types:

  1. CVCS: Centralized Version Control System

  2. DVCS: Distributed Version Control System

Centralized Version Control System

  • It is not locally available, meaning we have always needed to connected to the network to perform any action.

  • Since everything is centralized, if central server get failed you will loose entire data

  • Example: Concurrent Versions System (CVS), Perforce, and Subversion (SVN)

Distributed Version Control System

  • In DVCS, every contributor has a local copy or clone of main repository i.e everyone maintains a local repository of their own, which contains all the file and metadata present in the main repository.

  • Example: Git

What is git?

Git is a distributed version control system that tracks changes in any set of computer files, usually used for coordinating work among programmers who are collaboratively developing source code during software development.

What is gitHub?

GitHub is a platform and cloud-based service for software development and version control, allowing developers to store and manage their code.

Git three-stage architecture

3 Git – Three stage architecture - CodeTej

Some important Git command

  • To show all branches git branch

  • Create a new branch git branch <branch_name>

  • Command for branch merge git merge <branch_name>

  • git log --merge command helps to produce the list of commits that are causing the conflict

  • git diff command helps to identify the differences between the state's repositories or file

  • git checkout is used to undo the changes made to the file, or for changing branches

  • git reset --mixed is used to undo changes to the working directory and staging area

  • git merge --abort helps in exciting the merge process and returning back to the state before the merging began

  • git reset command is used at the time of merge conflict to reset the conflicted files to their original state

  • Set global username and email for Git (Locally).

      git config --global user.name "<your username>"
      git config --global user.email "<your email>"
    
  • Initialise an empty Git Repository

      git init
    
  • Clone an existing Git Repository

      git clone <repository URL>
    
  • Add file/stage to git

  •   git add <filename>
    
  • Add all the files to git

  •   git add .
    
  • Commit all the staged files to git

  •   git commit -m "<your commit message>"
    
  • Restore the file from being modified to Tracked

  •   git restore <filename>
      git checkout <filename>
    
  • Show the status of your Git respository

  •   git status
    
  • Show the branches of your git repository

  •   git branch
    
  • Checkout to a new branch

  •   git checkout -b <branch name>
    
  • Checkout to an existing branch

  •   git checkout <branch name>
    
  • Remove a branch from Git

  •   git branch -d <branch name>
    
  • Show remote origin URL

  •   git remote -v
    
  • Add remote origin URL

  •   git remote add origin <your remote git URL>
    
  • Remove remote origin URL

  •   git remote remove origin
    
  • Fetch all the remote branches

  •   git fetch
    
  • Push your local changes to remote branch

  •   git push origin <branch name>
    
  • Pull your remote changes to local branch

  •   git pull origin <branch name>
    
  • Check you git commits and logs

  •   git log
    

What is cherry picking in git?

Cherry picking is the act of picking a commit from a branch and applying it to another. git cherry-pick can be useful for undoing changes. For example, say a commit is accidently made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong.

Git stashing

git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit.

  • To stash an item git stash

  • To see stashed item list git stash list

  • To apply stashed item git stash apply stash@{<list number>}

  • To clear the stash items git stash clear

Happy Learning