Git Basics

Git is used as a version control software — used to maintain multiple versions of software.
But now, used for much more than that — collaborating with others, code versioning, sharing, deploying code from local machine to staging server, i.e., “deploying code to the cloud” and so on and so forth.

What does Git do?

What is the remote server that Git communicates with?

Git communicates with a remote server which hosts repositories (central storage place where all the code is kept and changes are tracked). A number of Git hosting sites include Github, Bitbucket, Gitlab, Beanstalk etc, that are used to store Git repositories on the internet for software development version control. We can “push” our repositories to the remote server so our collaborators and other people on the internet can see and work on those projects as well. Likewise, we can “clone” repositories of projects that we want to work on.

Git Commands

How do we send any changes we make locally to the hosting site?

Merging conflicts — Git will try to combine all the changes made. But there may be a conflict when multiple changes have been made to the same lines of a particular file which leads to a conflict when we try to run git pull. In such a case, the pull cannot happen since the merge cannot occur. A message appears that says, “automatic merge failed. You need to fix the conflicts and then commit the results.” After deciding what modifications are to be kept, the changes must be pushed.
Note: Any changes made to the repository on our computer does not affect what’s going on online on the host site unless we push those changes to it.

That’s all for now!

Create a remote, empty folder/repository on Github.

  1. Login to your Github account.

  2. At the top right of any Github page, you should see a '+' icon. Click that, then select 'New Repository'.

  3. Give your repository a name--ideally the same name as your local project. If I'm building a travel application, its folder will be called 'travel-app' on my computer, and 'travel-app' will be the Github repository name as well.

  4. Click 'Create Repository'. The next screen you see will be important, so don't close it.


Connect your local project folder to your empty folder/repository on Github.

The screen you should be seeing now on Github is titled 'Quick setup — if you’ve done this kind of thing before'.

Copy the link in the input right beneath the title, it should look something like this: https://github.com/mindplace/test-repo.git This is the web address that your local folder will use to push its contents to the remote folder on Github.

  1. Go back to your project in the terminal/command line.

  2. In your terminal/command line, type git remote add origin [copied web address]

Example: git remote add origin https://github.com/mindplace/test-repo.git

  1. Push your branch to Github: git push origin master

  2. Go back to the folder/repository screen on Github that you just left, and refresh it. The title 'Quick setup — if you’ve done this kind of thing before' should disappear, and you should see your files there.


  1. Create a new branch:
    git checkout -b feature_branch_name
  2. Edit, add and commit your files.
  3. Push your branch to the remote repository:
    git push -u origin feature_branch_name

It’s as simple as that!

What’s going on here? Git Branch explained in more detail

Git Branch

Git’s branching functionality lets you create new branches of a project to test ideas, isolate new features, or experiment without impacting the main project.

View Branches

To view the branches in a Git repository, run the command:

git branch

To view both remote-tracking branches and local branches, run the command:

git branch -a

There will be an asterisk (*) next to the branch that you’re currently on.

There are a number of different options you can include with git branch to see different information. For more details about the branches, you can use the -v (or -vv , or --verbose ) option. The list of branches will include the SHA-1 value and commit subject line for the HEAD of each branch next to its name.

You can use the -a (or --all ) option to show the local branches as well as any remote branches for a repository. If you only want to see the remote branches, use the -r (or --remotes ) option.

Checkout a Branch

To checkout an existing branch, run the command:

git checkout BRANCH-NAME

Generally, Git won’t let you checkout another branch unless your working directory is clean, because you would lose any working directory changes that aren’t committed. You have three options to handle your changes:

  1. trash them (see Git checkout for details 548) or
  2. commit them (see Git commit for details 1.1k) or
  3. stash them (see Git stash for details 588).

Create a New Branch

To create a new branch, run the command:

git branch NEW-BRANCH-NAME

Note that this command only creates the new branch. You’ll need to run git checkout NEW-BRANCH-NAME to switch to it.

There’s a shortcut to create and checkout a new branch at once. You can pass the -b option (for branch) with git checkout . The following commands do the same thing:

# Two-step method
git branch NEW-BRANCH-NAME
git checkout NEW-BRANCH-NAME

# Shortcut
git checkout -b NEW-BRANCH-NAME

When you create a new branch, it will include all commits from the parent branch. The parent branch is the branch you’re on when you create the new branch.

Rename a Branch

To rename a branch, run the command:

git branch -m OLD-BRANCH-NAME NEW-BRANCH-NAME

# Alternative
git branch --move OLD-BRANCH-NAME NEW-BRANCH-NAME

Delete a Branch

Git won’t let you delete a branch that you’re currently on. You first need to checkout a different branch, then run the command:

git branch -d BRANCH-TO-DELETE

# Alternative:
git branch --delete BRANCH-TO-DELETE

The branch that you switch to makes a difference. Git will throw an error if the changes in the branch you’re trying to delete are not fully merged into the current branch. You can override this and force Git to delete the branch with the -D option (note the capital letter) or using the --force option with -d or --delete :

git branch -D BRANCH-TO-DELETE

# Alternatives
git branch -d --force BRANCH-TO-DELETE
git branch --delete --force BRANCH-TO-DELETE

Compare Branches

You can compare branches with the git diff command:

git diff FIRST-BRANCH..SECOND-BRANCH

You’ll see colored output for the changes between branches. For all lines that have changed, the SECOND-BRANCH version will be a green line starting with a “+”, and the FIRST-BRANCH version will be a red line starting with a “-”. If you don’t want Git to display two lines for each change, you can use the --color-words option. Instead, Git will show one line with deleted text in red, and added text in green.

If you want to see a list of all the branches that are completely merged into your current branch (in other words, your current branch includes all the changes of the other branches that are listed), run the command git branch --merged .

Update a Branch from Remote

To update a local branch from remote:

git stash (optional, to save local changes which differs from the remote repository if any) 

If you weren’t already on the branch you want to work on:

git checkout my_local_branch 

Finally pull from the remote branch

git pull

Track a Remote Branch

If you already have a branch and you want to track a remote branch, then you use set-upstream-to command:

git branch --set-upstream-to origin/BRANCH

Or you can use the -u flag (upstream) when you make your first push:

git push -u origin BRANCH

Help with Git Branch

If you forget how to use an option, or want to explore other functionality around the git branch command, you can run any of these commands:

git help branch
git branch --help
man git-branch

Towards 

Create a remote, empty folder/repository on Github.

  1. Login to your Github account.

  2. At the top right of any Github page, you should see a '+' icon. Click that, then select 'New Repository'.

  3. Give your repository a name--ideally the same name as your local project. If I'm building a travel application, its folder will be called 'travel-app' on my computer, and 'travel-app' will be the Github repository name as well.

  4. Click 'Create Repository'. The next screen you see will be important, so don't close it.


Connect your local project folder to your empty folder/repository on Github.

The screen you should be seeing now on Github is titled 'Quick setup — if you’ve done this kind of thing before'.

Copy the link in the input right beneath the title, it should look something like this: https://github.com/mindplace/test-repo.git This is the web address that your local folder will use to push its contents to the remote folder on Github.

  1. Go back to your project in the terminal/command line.

  2. In your terminal/command line, type git remote add origin [copied web address]

Example: git remote add origin https://github.com/mindplace/test-repo.git

  1. Push your branch to Github: git push origin master

  2. Go back to the folder/repository screen on Github that you just left, and refresh it. The title 'Quick setup — if you’ve done this kind of thing before' should disappear, and you should see your files there.

Science

Sharing concepts, ideas, and codes.





Git Homework - navjot bansal

Git Blame

From first user (shadow) made a file touch.cpp

[shadow@master oracle_training]$ vi touch.cpp
[shadow@master oracle_training]$ git add touch.cpp 
[shadow@master oracle_training]$ git config --global user.email "shadow@gmail.com"
[shadow@master oracle_training]$ git config --global user.name "shadow"
[shadow@master oracle_training]$ git commit -m "base c++"
[master 64365c0] base c++
 1 file changed, 6 insertions(+)
 create mode 100644 touch.cpp
[shadow@master oracle_training]$ git push origin master
Username for 'https://github.com': navjotbansal
Password for 'https://navjotbansal@github.com': 
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 367 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/NavjotBansal/oracle_training
   a43f4c3..64365c0  master -> master
[shadow@master oracle_training]$ ls
story2.txt  story.txt  touch.cpp
[shadow@master oracle_training]$ cat touch.cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
  return 0;  
}

From first user (navjotbansal) made a file touch.cpp

[root@master test]# git pull origin master
From https://github.com/NavjotBansal/oracle_training
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 touch.cpp | 6 ++++++
 1 file changed, 6 insertions(+)
 create mode 100644 touch.cpp
[root@master test]# ls
dev.txt  story.txt  story2.txt  touch.cpp
[root@master test]# vi touch.txt
[root@master test]# rm touch.txt 
rm: remove regular empty file 'touch.txt'? 
[root@master test]# vi touch.cpp
[root@master test]# git add touch.cpp 
[root@master test]# git commit -m "added headers and IO file"
[master caaab03] added headers and IO file
 1 file changed, 3 insertions(+)
[root@master test]# git push origin master
Username for 'https://github.com': navjotbansal	
Password for 'https://navjotbansal@github.com': 
Counting objects: 11, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 921 bytes | 0 bytes/s, done.
Total 8 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/NavjotBansal/oracle_training.git
   64365c0..caaab03  master -> master
[root@master test]# git blame touch.cpp 
64365c09 (shadow       2020-09-29 04:05:09 +0530 1) #include<bits/stdc++.h>
64365c09 (shadow       2020-09-29 04:05:09 +0530 2) using namespace std;
caaab03c (navjotbansal 2020-09-29 04:10:10 +0530 3) #define vi vector<int>
64365c09 (shadow       2020-09-29 04:05:09 +0530 4) int main()
64365c09 (shadow       2020-09-29 04:05:09 +0530 5) {
caaab03c (navjotbansal 2020-09-29 04:10:10 +0530 6)   ios_base::sync_with_stdio(
caaab03c (navjotbansal 2020-09-29 04:10:10 +0530 7)   cin.tie(false);cout.tie(fa
64365c09 (shadow       2020-09-29 04:05:09 +0530 8)   return 0;  
64365c09 (shadow       2020-09-29 04:05:09 +0530 9) }

Git grep

[root@master test]# git grep "it"
story.txt:right commit
story2.txt:wrong commit
touch.cpp: #include<bits/stdc++.h>
touch.cpp:  ios_base::sync_with_stdio(false);
[root@master test]# 

Git describe

[root@master test]# git tag -a v1.0 -m "version1.0"
[root@master test]# git push origin --tags
Username for 'https://github.com': navjotbansal
Password for 'https://navjotbansal@github.com': 
Counting objects: 1, done.
Writing objects: 100% (1/1), 154 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/NavjotBansal/oracle_training.git
 * [new tag]         v1.0 -> v1.0
[root@master test]# touch second_version.py
[root@master test]# vi second_version.py
[root@master test]# git add second_version.py 
[root@master test]# git commit -m "uuid"
[master c32d9a9] uuid
 1 file changed, 4 insertions(+)
 create mode 100644 second_version.py
[root@master test]# git push origin master
Username for 'https://github.com': navjotbansal
Password for 'https://navjotbansal@github.com': 
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 312 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/NavjotBansal/oracle_training.git
   caaab03..c32d9a9  master -> master
[root@master test]# git tag -a v1.1 -m "version1.1"
[root@master test]# git show-ref
11a2ed6a7c0cec8bb63dfec211ec56bad179ae14 refs/heads/dev
c32d9a9b57610c7268b079d1c0dbec17ef7346c4 refs/heads/master
c32d9a9b57610c7268b079d1c0dbec17ef7346c4 refs/remotes/origin/master
d9e183ce8ea7134f1a46c14104c00cfa98de6db9 refs/tags/v1.0
e21744c39d9617ea7c9ee2ac89c92cf1d8c86bf0 refs/tags/v1.1
[root@master test]# git push origin --tags
Username for 'https://github.com': navjotbansal
Password for 'https://navjotbansal@github.com': 
Counting objects: 1, done.
Writing objects: 100% (1/1), 155 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/NavjotBansal/oracle_training.git
 * [new tag]         v1.1 -> v1.1
[root@master test]# git ls-files
dev.txt
second_version.py
story.txt
story2.txt
touch.cpp
[root@master test]# git tag -l
v1.0
v1.1
[root@master test]# git describe
v1.1


git init \
2. git config \'a1\'aaglobal user.name \'a1\'b0name\'a1\'b1\
3. git config \'a1\'aaglobal user.email \'a1\'b0email@xmu.edu.my\'a1\'b1\
4. git remote add origin <url> //\'d5\'e2\'b8\'f6\'ce\'d2\'c0\'ed\'bd\'e2\'ce\'aa\'b0\'d1\'b1\'be\'b5\'d8\'bf\'e2\'d3\'eb\'c4\'e3\'d2\'aa\'c9\'cf\'b4\'ab\'b5\'c4\'d4\'b6\'b3\'cc\'b6\'cb\'bd\'f8\'d0\'d0\'b0\'f3\'b6\'a8\
5. git status //\'d5\'e2\'b8\'f6command\'ca\'c7\'b2\'e9\'bf\'b4\'d7\'b4\'cc\'ac \
#5. git add . //\'d5\'e2\'b8\'f6\'ca\'c7\'cc\'ed\'bc\'d3\'cb\'f9\'d3\'d0\'a3\'ac\'c4\'e3\'d2\'b2\'bf\'c9\'d2\'d4\'d6\'b8\'b6\'a8\'cf\'e0\'b9\'d8\'ce\'c4\'bc\'fe\
#6. git commit -m \'a1\'b0\'d5\'e2\'c0\'ef\'c3\'e6\'ca\'c7\'c4\'e3\'b5\'c4\'b1\'b8\'d7\'a2\'a1\'b1\
#7. git push origin master //\'cc\'ed\'bc\'d3\'b5\'bdmaster\'b5\'c4\'d2\'bb\'b8\'f6branch\



Git Bisect

( after the version 1.0 tag uuid.py file was added and pushed to v1.1 )

as version 1.0 was good

so git showed uuid file to the bug ridden code of version post 1.0

[root@master test]# git bisect start 
[root@master test]# git bisect bad
[root@master test]# git bisect good v1.0
c32d9a9b57610c7268b079d1c0dbec17ef7346c4 is the first bad commit
commit c32d9a9b57610c7268b079d1c0dbec17ef7346c4
Author: navjotbansal <nav@bansal.com>
Date:   Tue Sep 29 04:34:17 2020 +0530

    uuid

:000000 100644 0000000000000000000000000000000000000000 82994204e210f219
a4efdb7664f18435fa0f4733 A	second_ve

Comments

Popular posts from this blog

Flutter for Single-Page Scrollable Websites with Navigator 2.0

A Data Science Portfolio is More Valuable than a Resume

Better File Storage in Oracle Cloud