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.

To stage changes for selective file

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

To stage all changes (not specific file) and commit

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

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

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) :

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:

So to do this:

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

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

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

To view branches (* will be current branch)

To switch branch

To delete branch (here it is named develop)

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

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

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

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

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

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

To see branches and their last commits

To see which branches are merged with me.

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.

Pushing Git Branches To Remote Repositories

If your want to publish your repository, github is the most popular medium to do this. This is called git remote. Lets do this. If you are doing this for the first time, it is little stressful, but don’t worry. Lets do this.

Configuring Github (Skip this step if you aren’t first timer)

Firstly create a github account. Create Repository and get the link of that repository. Get the SSH link (not HTTPS). Here mine was git@github.com:kawshikbuet17/GitTutorialDemo.git

Lets publish our git repo to github. Run the commands below.

git status

working tree must be clean. If not, then commit your changes.

git remote add origin git@github.com:kawshikbuet17/GitTutorialDemo.git

git remote -v

Okey. Added our remote. Now we can push it to our github (remote). Lets do it.

git push -u origin master

It will not work for now. You have to tell your github about your PC that this is your PC. This is done by adding SSH key in github. SSH key options is in the settings of github. You have to run some commands into your git bash and get SSH key and then add it to your github account to verify your PC to your github account. After you do it once, you don’t have to worry about this in future.

Run the following commands without thinking anything.

ssh-keygen -t rsa -b 4096 -C “kawshik.kumar.paul@gmail.com”

This output may not be appeared in your git bash. Someday I have done something maybe. but still follow the syntaxes.

eval $(ssh-agent -s)

ssh-add ~/.ssh/id_rsa

tail ~/.ssh/id_rsa.pub

Copy this full key and add it to SSH key in github settings. Then you are done.

Now our push in github will work. Lets see.

This is to be done for the first time. Later, you don’t have to add SSH key. git push -u origin master will work directly.

Working with Git Remote (GitHub)

If your have a git repository in your local machine, you can publish it to remote repository like this.

Lets go to our github repository and see the repository. It will work fine if you don’t make any mistake.

Here only master will be pushed. Other branches will not be pushed. We have to push other branches separately. Lets do it. Suppose we had a branch named bugfix.

First switch to the branch using

Now here we can see that bugfix branch has been pushed to github.

If ‘able to merge’ arrives in this page, it means that we can merge it without any conflict.

Notice: While pushing a branch, be in that branch (It is recommended). Otherwise problems may occur. such as notice this thing:

But if we do:

Then our bugfix will be added into github with the name of mybugfix as another branch. This is dangerous.

Now we will merge bugfix and delete bugfix (if we delete before merge, we will get error). After merging, deleting bugfix and pushing, in github, still there will be bugfix. But merge is successful (I can see the commit of bugfix in master branch, notice this.txt)

In github there are the branches but these are not in git (we deleted bugfix from git). But we didn’t deleted from github, so it is showing still. Now we will delete it.

To delete branch from github (means git origin):

Delete Remote of Git

That means will will not access github repository for this git. to do this:

It can be added again.

Deleted all the files and pushed it (Published Commit). Now how to get back?

https://stackabuse.com/git-revert-to-a-previous-commit/

Now we want to get back to “commit 3 after delete”. So we have to delete the commit 5 and commit 4 — this 2 commits.

git reset HEAD~2 will delete last 2 commits.

git reset HEAD~n here you can choose your n , to delete last n commits. btw, this is not deleting commit. This is only point the head back to previous. To understand this you can run git reflog . You will understand this thing very clearly.

So last 2 commits are deleted.

Now suppose you have now pushed the current commit (commit 3 after delete) into remote (origin). Suddenly you realized that you have to go to the “merged issue 1” commit and discard the commits after it. How to do it?

We have to delete 4 commits from “commit 3 after delete” to “deleted all things”.

If you have to delete the commits which had been pushed in github, then you have to make a force push in github after reseting head. If the things were not published, you don’t have to run force push. force push means kind of forcefully pushing whatever happens. To make force push:

git push -f origin master

After force pushing, you will see that github will go to that point which you wanted. If you don’t make force push, then changes will come to git, but will come in github. So you must make force push sometimes.

Git Clone

You can clone a public repository and work on it separately.

git clone https://github.com/haris989/TextUtils.git mytextutils

Here note that, while cloning in a directory (Here NewDir), the directory is not a git repo. But after cloning, get into the folder which is got by cloning. Then you will see that is a git repo.

You can do everything here which can be done in a git repo.

Btw, if you want to work in a project, one of your project partner must make a github repository and make you contributor. Then you have to clone that project and work on it. If you are a contributor, then you can make changes in master. You can make push and pull. Process are all same, but you need to know about pulling only. git pull to get the changes by others, git push -u origin master is to push your changes in github.
That’s it.

I will develop this note time to time with many details and corrections. You can see more from this https://paste.ubuntu.com/p/vnyMsXbFTj/

Thanks for reading.

CS Undergrad | BUET

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store