Git Basics


git is a version control system. It is a software developed by the same guy who created Linux operating system. His name is Linus Torvalds.

Now the question is what is version control system. A system that controls the versions of our code. Let's take a scenario. We are two guys creating an application. Both of us are working in backend. There is a new feature to be developed by both of us. I have done a part of work then you can use my part and integrate with yours. And if you have developed something, I can use it. But let's say there is a problem in my code. We can get back to a position where we can have a fresh code from that point.

we can have two types of repository:
  • local repository
  • remote repository
The remote repository is an optional repository. It is used to keep the work safe. Additionally, it is used as a reference by our peer developers. They can use the remote repository to make a copy of it for them selves. They can push the changes from there local to remote repository. We can use there changes using remote repository.

The local repository is used for development of an individual developer.

Steps to install git software on a Linux based operating system:

Check if git is installed
which git 
or
git --version

Help manual commands
git help
git help <command>
e.g.
git help init

If the git manual is unavailable, we can use below command to get the manual. (Need to check)
sudo apt-get install git-man

Three areas:
Working area - here we make changes to our file in the repository
Staging area - here we add the only files to be placed in the commit
Committed area - this records the version of the files committed.

Initialization in a directory:
If we want to make our directory to be version controlled, we can initialize it with git. Below steps to follow. 
git init

----
Working area:
>> touch story1.txt
>> echo "This is my beautiful story" >> story1.txt
>> git status


On branch master {default branch}

No commits yet (there is no previous commit)

Untracked files: (git does not know what to do with these files)
  (use "git add <file>..." to include in what will be committed)
        story1.txt

nothing added to commit but untracked files present (use "git add" to track)
----
>> git add style1.txt
Staging area

----
>> git commit -m "Add to my story"
Commit area

When we check status, we can see how far our commit is from remote branch. 
Working area > staging area > commit

commit command can be use without message as well. This will open the default editor for the message. 
It is recommended to use the commit for solving a single problem. This will make tracking easier. 

Commit log
git log
fatal: your current branch 'master' does not have any commits yet
(When there is no commit, we get above message.)

git log --oneline
git log --name-only # to check filenames as well
git log -n 3 # get last three commits
git log --decorate 
git log --graph --decorate # to check from which branch the current branch was created. 
git log origin/master # to check origin/master branch logs

We can change the branch and untracked files are untracked which means we can move around the branches with untracked files. 

Branch:
git branches are nothing but pointer to a commit (commit is a version of a state)

Create a branch
git branch <branch-name>

Switch to a branch
git checkout <branch-name>

Create and immediately switch to the branch
git checkout -b <branch-name>

Delete a branch
git branch -d <branch-name>

List branches
git branch
or
git branch -a # for remote branch as well

Head is the place where are currently pointing to a commit. The head switches as we switch the branch. 

Merging the changes of branch to another branch:
When we are working on feature branch, we want to ensure it gets merged with our master branch or any important branch. For that, we need to perform two steps process. First we need to checkout the branch where we will be merging. Secondly, we will run the merge command to get our changes merged to current branch.
Step1: Checkout
git checkout <branch-in-which-we-will-merge>
Step 2: Merging
git merge  <branch-that-needs-to-be-merged-with-checkout-branch>

There are two types of merging:
fast-forward merge: It is done where there is no extra commit. 
no fast-forward merge: It is done when there are extra commits to the checked out branch. 

Remote Repository:
There are platforms to host a remote repository such as GitHub, GitLab, Bitbucket. We can upload our repository in these platform. They provide a connection string (URL). Using this URL/connection string, we can communicate with remote repository. 
In order to connect with a remote repository, we can use below command
git remote add origin <connection string>
origin is an alias here. 

To list the remote repositories locally:
git remote -v

Remote Repository clone:
If a new member wants to get a copy of remote repository, they can use ssh method of cloning the repo. Every remote hosting website has a different place for SSH clone link. For GitHub, it is under repo and green button. Use below command to get the clone:
git clone <repo-ssh-link>

- once a repo is set with  with the connection string by a user, another use does not need to add the same. It  comes along with git. 
- In order to push changes to a repository, the user must define the configuration email and username. 
- Also, to push changes, the user can be added by admins to perform the write operation. This is necessary, otherwise one cannot push the changes.

Pull Request:
The pull request or PR is a feature to merge to master branch. We used an option merge in the local repository. In order to merge to master branch in the remote repository, we can open a PR and give it for a review to our teammates. They can comment and give feed back. Once you address them, the PR can be merged by you. If you don't have write permission in the repo, you need to request a user with write permission to merge for you. 

Pull changes from remote:
The local repo is generally not aware of remote changes. In order to fetch the changes from the remote repository, we can perform a two steps process. 
step 1: Fetch the remote changes
git fetch origin master
step 2: merge the changes (origin/master is the name of the branch)
git merge origin/master

Or we can combine above two steps and run below commands
git pull origin master

Remote branches has remotes in them
sarah (master)$ git branch -a
* master
  story/frogs-and-ox
  remotes/origin/master

Merge Conflict:
When two different changes are being merged. Git generally is not able to decide which one to keep. In such case, git gives hint of incoming changes and our current change with >>>>>Head ===== <<<<<<

Here we need to choose which version to keep. Once we choose the preferred version of the file, we can add and merge the file to the branch. 

Forking remote Repository:
In open source projects, not every has membership to contribute to it. One cannot create a branch or Pull request in an open source. If they want to contribute to open source, they can first make a fork of a repository. Forking is kind of copying for the self. And experiment and test the changes. Once they are ready to add the changes, they can raise a Pull request to original repo from the forked repo. (How?)

Raise a PR from jon's forked repo to sarah's repo.


How to raise a PR from a forked repo to original repo
Login to forked repo with your own user
Create a new PR with following information
Merge into(destination) branch: sarah:master
Pull from(source) branch: jon:master

Rebase:

Cherry-pick a commit from a branch:
git cherry-pick 0706b82 # the hash can be from any branch that contains the changes we need. 

Revert or reset changes from a commit:
It can happen that we committed changes that we did not want to commit and mistakenly it went to our commit history. We have two options, either we keep the history and create a new commit of reverted change, or we remove the commit itself. 
Revert Commit changes with history
git revert <commit-hash>

Reverse Commit change without history:
Softer way - we will have the files from the removed commit. 
git reset --soft HEAD~1

Harder way - we will remove from the files the commit as well.
git reset --soft HEAD~1

Stash (preserve) the changes
If we want to save changes temporarily, we can stash them. This will allow us to move to a different branch. 
git stash 

to list all the stashed changes
git stash list

to apply specific stashed changes
git stash <specific stash>

to check files in specific stashed changes
git stash show stash@{0}

Get the history of every action performed
git reflog

Basic commands

Cloning a repo
git clone <repo-link>

Switch to a branch
git checkout <branch-name>

Checking the status of files changes
git status

Pull changes from a specific branch
git pull origin <specific-branch-name>

Pull changes from the current branch
git pull

Temporary removing the current changes
git stash

Comments

Popular posts from this blog

Linux Basics

Internet basics