Git Basics
What does Git do?
- Suppose starting out with the first version of code, Git allows us to have access not just to the latest version of our code but all the lines added/removed are tracked and we know all about the history of our project.
- Git is very good at synchronising code between different people. Git stores the code in a remote server and so other people we are working with have access to the same code. When multiple people make changes to the code, Git retains an updated version of the code that merges all the changes made, so everyone in the team is on the same page.
- Git is very good at testing code. While making changes to the code and testing to see how well/if it works, Git allows us to maintain an original copy of the code. We can revert to it after testing new pieces of code or if happy with the new code, replace it with the original. Hence, Git is great at allowing to revert to old versions of code.
What is the remote server that Git communicates with?
Git Commands
git clone <url>
: url of the repository- hosted on Github/Bitbucket or any other remote server- that we want to download on our computergit add <filename>
: this file is a file that we want to track the next time we save a copy of the repository. Remember that we have to be in the same directory as the one that contains the file to be added.git add *
: adds all files that are unsaved which is obviously faster than git adding individual files. However, separating the adds and commits gives us control over our repository and enables us to specify which changes we want to keep track of.git commit -m "message"
: this command simply saves the changes made to the repository. -m stands for method. The message should describe whatever we’ve changed in the “commit”. This is very useful as we can go back to the list of messages describing a sequential set of changes.git status
: tells us what is currently going on in our repository
How do we send any changes we make locally to the hosting site?
git push
: pushes the code from our computer onto the repository that’s stored on the internet. As a result, the host side, example GitHub, is now able to keep track of it.git pull
: this command is used to download the latest version of a repository on our local computer.
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.git log
: shows a history of all the commits made. Each commit is distinguishable by a unique commit hash.git reset --hard <commit hash>
: resets the repository back to the version specified by that commit hash.git reset --hard origin/master
:origin/master
denotes the version of the repository that was the origin of the repository where we got it from.
push
those changes to it.Create a remote, empty folder/repository on Github.
Login to your Github account.
At the top right of any Github page, you should see a '+' icon. Click that, then select 'New Repository'.
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.
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.
Go back to your project in the terminal/command line.
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
Push your branch to Github:
git push origin master
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.
- Create a new branch:
git checkout -b feature_branch_name
- Edit, add and commit your files.
- 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:
- trash them (see Git checkout for details 548) or
- commit them (see Git commit for details 1.1k) or
- 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.
Login to your Github account.
At the top right of any Github page, you should see a '+' icon. Click that, then select 'New Repository'.
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.
Click 'Create Repository'. The next screen you see will be important, so don't close it.
Login to your Github account.
At the top right of any Github page, you should see a '+' icon. Click that, then select 'New Repository'.
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.
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.
Go back to your project in the terminal/command line.
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
Push your branch to Github:
git push origin master
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