GIT | Basics

Talha nousher
3 min readJul 23, 2022

--

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

The Git feature that really makes it stand apart from nearly every other SCM out there is its branching model.

Git allows and encourages you to have multiple local branches that can be entirely independent of each other. The creation, merging, and deletion of those lines of development takes seconds.

Here are some basic things that helps you in using git and managing your code appropriately.

Basics

  • Initialisation i-e git init
  • Adding files i-e git add < filename|filenames|specifiers >
  • Set remote i-e git remote add < remote-name > < remote-path >
  • Commit files i-e git commit -m < commit-message >
  • Push files i-e git push < remote-name > <branch-name >

How to change/modify commit?

The git commit command captures a snapshot of the project's currently staged changes. Committed snapshots can be thought of as “safe” versions of a project—Git will never change them unless you explicitly ask it to. Prior to the execution of git commit, The git add command is used to promote or 'stage' changes to the project that will be stored in a commit. These two commands git commit and git add are two of the most frequently used.

  • After a good few hours of coding, it’s easy for a spelling error to sneak into your commit messages. It may be a spelling mistake or messages like ‘fix’ , ‘fixes’ and ‘adjustments’ which need to be correct. It may be some files which you forgot to add in a commit.
  • Modify commit i-e git commit –amend
  • Modify commit message i-e git commit –amend -m “message”

Branching

  • Root branch i-e main | master
  • Create new branch i-e git branch < branch-name >
  • Switch to a branch i-e git checkout < branch-name >
  • Create and switch to a new branch i-e git checkout -b < branch-name >

Stashing

  • Stores uncommitted ( stages or unstaged ) changes i-e git stash
  • Stores untracked files i-e git stash — include-untracked
  • Stores untracked and ignored file i-e git stash –all
  • Listing stashes i-e git stash list
  • Retrieving stash changes i-e git stash apply or git stash pop
  • Clear Stash i-e git stash clear or git stash drop < stash-id >

Revert

  • Revert to previous commit i-e git revert HEAD.
  • Revert to specific commit i-e git revert HEAD~< commit number >.
  • Revert specific commits i-e git revert < branch name >~< commit number >..< branch name >~< commit number >.

Merge

  • The first method to combine work that we will examine is git merge. Merging in Git creates a special commit that has two unique parents. A commit with two parents essentially means “I want to include all the work from this parent over here and this one over here, and the set of all their parents.”
  • Merging i-e git merge < branch >

Rebase

  • Rebasing is the process of moving or combining a sequence of commits to a new base commit. Rebasing is most useful and easily visualized in the context of a feature branching workflow.
  • Rebasing is one of the method that is used for combining the work between branches.
  • The advantage of rebasing is that it can be used to make a nice linear sequence of commits. The commit log / history of the repository will be a lot cleaner if only rebasing is allowed.
  • Rebasing i-e git rebase < branch >

Cherry Pick

  • 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 accidentally made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong.
  • Cherry Pick i-e git cherry-pick <commit-id> <commit-id> .. <commit-id>

These are the few things I try to explain about git and hoping it ay helps yo in your development career.

Feel free to reach me out at

Any recommendations are welcome.

Thanks

--

--

Talha nousher

Bachelors in Computer science. JavaScript developer, Working as MERN stack developer. Interested in writing tech articles related to node.js and react.