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 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:
Set your username:
git config --global user.name "kawshikbuet17"
Set your email:
git config --global user.email "email@example.com"
Now to see git configuration details
git config --list
Now go to the specific directory which you want to track with git. Then open git bash there and run this command.
Git will not make any directory automatically tracked. You must tell git to track your directory by a simple command:
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
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
git add fileName
To commit the changes of staged file (unstaged files will not participate here):
git commit -m "your commit message here"
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
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:
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).
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.
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)
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 .
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
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.
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
Lets publish our git repo to github. Run the commands below.
working tree must be clean. If not, then commit your changes.
git remote add origin firstname.lastname@example.org: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 “email@example.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)
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
git checkout bugfix
git push origin bugfix
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:
git push origin bugfix:mybugfix
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):
git push -d origin bugfix
Delete Remote of Git
That means will will not access github repository for this git. to do this:
git remote rm origin
It can be added again.
git remote add origin url_link_here
Suppose we have to delete 4 commits from “commit 3 after delete” to “deleted all things” and go to the “merged issue 1” commit and discard the commits after it. How to do it?
Just get the commit id and run :
git reset --hard commit_id_here
Here my code will be:
git reset --hard 12ee2ae
After running this syntax you will get back to that commit. And you can work it from there. But if you published it, you must make a force push to make the changes in GitHub. To make that force push, run:
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.
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.
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.