Understanding Git Basics -Part-2

Tharuja Sandeepanie
4 min readFeb 12, 2020

--

In this article , I’m focusing on basic Git commands , which would be helpful for you when working with Git. From my previous article you can get a basic idea about working with git repositories :- link

Initialize a local repository

From my previous article I described how to set up a Git repo by cloning. Now we will see, how to initialize a local repository from existing code in your machine.

In the terminal navigate to your project directory and give :-

git init

This will create a .git folder in your working directory and create a new master branch. Now your project can be tracked by Git. If you have some files/folders which are not needed to track by Git, then simply create a .gitignore file and give those files. So, those files will not be tracked by Git.

Now do “git add” and “git commit” commands as I described in my previous article. Add .gitignore file too.

Connect local repository with remote repository

Now we want to add your local repo to Github, first you must create a remote repo in Github. Go to your Github account and create a new repo as below.

Now , give below commands to add your remote repo as an origin and push the committed files into that remote repo.

git remote add origin https://github.com/Tharuja/Hello--World.git
git push -u origin master

Refresh your created remote repo in Github and you can see your local project has been added to it.

Create a new branch

By default , you are in the branch named “master” and you can create different branches for your desired features.

git branch <branch-name>  // Create a new branch
git checkout <branch-name> // Switch to a branch
git checkout -b <branch-name> // Create a new branch and switch to that branchgit checkout -b <branch-name> origin/<branch> //Break a new branch from a given repo & branch , can use upstream repo too.git branch //List down all the branches in local repo
git branch -a // List down all the branches including remote branches.

After switching to a new branch you can add and commit the changes you did and do “git push origin <new-branch>” . It will create such a new branch in your remote repo with those changes.

Merge a branch

Suppose you created a new branch named “test” and did some changes there.And if you want to merge this test branch to master branch follow below steps.

git checkout master
git pull origin master // this is only to get the latest updates of master branch
git merge test(branch you want to merge) //test branch will be merged into master in your local repo.git push origin master //Push those changes to origin/master branch.

Delete a branch

If you want to delete a specified branch then follow below steps:

git branch -d <branch-name> git branch -a  // You can see this branch has been deleted from local repo but it is still in remote repogit push origin --delete <branch-name>  // Delete that branch from remote repo.

Other basic Git commands :-

-To check your current version of Git in your machine
git --version
-To get the manual page for the git commands
git help
-Show the state of the working directory and the staging area, which branch you are currently in.
git status
-To display all of the commits in a repository’s history.(each commit’s: Secure Hash Algorithm (SHA),author,date,commit message)
git log
-To undo a commit
git revert <commit hash>

Git stash is used to temporarily save changes (both staged and unstaged) you’ve made to your working directory for later use and then you are free to do changes, switch branch and pull updates..etc. Stash is local to your Git repository; stashes are not transferred when you push.

git stash//Apply the stashed changes again and keep them in your stash
git stash apply
//Apply the stashed changes again and remove them from stash
git stash pop

--

--