Git and GitHub Handbook
By: Kawshik Kumar Paul
Tags: Git Bash, Github, Remote Working, Git

This is a short handbook for git and github. It covers the basic things which are needed to know while working with git and github.

Git is an open-source, version control tool. GitHub is a code hosting platform for version control and collaboration.

Install Git

install git scm from https://git-scm.com/

I am using git version 2.29.2.windows.1 here.

Open Git Bash from Start Menu.

To check your version after installation:

git --version

Set your username:

git config --global user.name "kawshikbuet17"

Set your email:

git config --global user.email "kawshik.kumar.paul@gmail.com"

Now to see git configuration details

git config --list

Initialize Git

Now go to the specific directory which you want to track with git. Then open git bash there and run this command.

git status

Git will not make any directory automatically tracked. You must tell git to track your directory by a simple command:

git init

git init is an initial command of a repository to track. After running this command, your repository is a git repository. Now you start work.

Lets add some files and check git status .

So, our journey of git has been started. git status command is a command which you must know even if your forget your own name.

If you have come to this point, then your are successful. The rests are not so hard.

Commit Changes (That means save-points)

After going to a directory anytime, the first command you must run is git status to see the condition of your directory.

Here note that working tree is clean means all changes is tracked successfully. untracked file means we didn’t add it for tracking. modified means we added for tracking but didn’t commit the changes. stage means to add for tracking, unstage means not to add for tracking.

Anyway, let’s get started with a directory.

git status

To stage changes for selective file

git add fileName

To commit the changes of staged file (unstaged files will not participate here):

git commit -m "your commit message here"

Working tree clean means, all changes commited

To stage all changes (not specific file) and commit

git add . git commit -m "type commit message here"

After commit, working tree will be clean, that means commit successful.

This can be done faster with one command (skipping git add . command)

git commit -a -m "type here commit message"

Suppose you want to discard the changes you have made (and didn’t staged it I mean didn’t add it) and want to get back like last commit (I mean last save-point) :

git restore filename

or,

git checkout filename (don't use this)

But suppose, you haved staged the changed file and you want to discard the change, here restore must be used. Don’t use checkout. It won’t work here. See this:

here checkout won’t work

So to do this:

git restore --staged fileName

git restore fileName

Here note that: If you are using VS Code and unstaged a change and came back to last commit, then VS Code will show the unstaged changes of your files with unsaved tag. Don’t save it if you want to get back to last commit and discard the new changes (it needs some practice to understand this clearly).

Git Ignore

If you want to ignore tracking a file, you have to use gitignore.

Make a file named “.gitignore” . This is a text file. The path added in this file will be avoided tracking by git.

If inside .gitignore I write

kawshik.txt

myenv/

*.jpg

kkp/paul/

kkp/kawshik.png

Here myenv folder, paul folder, kawshik.txt file, all .jpg files, kawshik.png file will be avoided tracking by git.

Now suppose if you tracked a file previously and want to stop tracking now, run

git rm -r --cached filenName

Otherwise, it won’t stop tracking though you added to gitignore.

It’s better to add gitignore and then create some file.

Deleting Git Repo

If you want to stop a directory from git, run rm -rf .git

This mainly delete the .git folder.

Git Branch

Branching is very important to make experiments and then merge the experiments to the main. Here the main branch is called master. Unless we add and switch to a branch, our initial branch is always master branch. Whatever, lets create branches.

To create branch

git checkout -b branchName

After creating branch, git automatically switches to new branch. Here branchName I used is issue1.

To view branches (* will be current branch)

git branch

To switch branch

git checkout switchToWhatBranch

To delete branch (here it is named develop)

If the branch named develop is already merged with the master, then run

git branch -d develop

If it it not merged with the master but you still want to delete, then run

git branch -d develop

git branch -D develop

Merging and Merge Conflicts

Merge conflict means, suppose you are working in two branchs parallelly and made changes to same portion of a file or something, then git won’t understand which changes to keep. This is called merge conflict. If merge conflict happens, git will make you choose about what changes should be kept.

Here in VS Code you can choose “Accept Current Change”/ “Accept Incoming Change” etc or you can change it manually but editing code. I clicked on Accept Incoming Change:

Now run git status, it will show I have resolved merge conflicts but didn’t added the new changes.

We have to write

git add .

Or,

git add index.html

Then we have to commit. Then merging will be completed.

We can see git log to see all commits including this. Run

git log

We are inside git log now. To exit, type ‘q’.

To see branches and their last commits

git branch -v

To see which branches are merged with me.

git branch --merged

git branch --no-merged

Notice the difference of previous picture and this picture

So, calculate the merge operation while in master, otherwise it will be difficult to calculate what is happening.

Here, issue1 is merged with the master, issue2 isn’t. So deleting issue1 will make no loss but be alert while deleting issue2 because it is not merged. Before merging, deleting branch may occur some data losses. So be alert. Git will give error while deleting unmerged branch, won’t give error while deleting already merged branch.