Assuming git is installed on your machine, else please install it from here Git Scm
Please open git-bash
Let us start with an empty directory
1
2
$ mkdir git_tutorial
$ cd git_tutorial
- Add a file in that directory
1
2
3
$ touch a_file
$ ls
a_file
- Initialize git
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ git init
Initialized empty Git repository in /Users/rahul.chandna/git_tutorial/.git/
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
a_file
nothing added to commit but untracked files present (use "git add" to track)
- Add which means to “Stage” files locally
1
2
3
4
5
6
7
8
9
10
$ git add a_file
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: a_file
- commit file locally
1
2
3
4
5
6
7
8
$ # staged
$ git commit -m "initial commit"
[master (root-commit) 002d6e8] initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a_file
$ git status
On branch master
nothing to commit, working tree clean
- add changes to file locally
1
2
3
4
5
6
7
8
9
10
$ echo "first line" > a_file
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: a_file
no changes added to commit (use "git add" and/or "git commit -a")
- view changes in file and add/stage it locally
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ git diff a_file
$ git add a_file
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: a_file
$ git commit -m "added first line to the file"
[master d71d315] added first line to the file
1 file changed, 1 insertion(+)
$ git status
On branch master
nothing to commit, working tree clean
- view the git history of commits
1
$ git log
- add more changes to file locally, view difference and commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ echo "second line" >> a_file
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: a_file
no changes added to commit (use "git add" and/or "git commit -a")
$ git diff a_file
$ git add a_file
$ git commit -m "added second line to the file"
[master 3be860e] added second line to the file
1 file changed, 1 insertion(+)
$ git status
On branch master
nothing to commit, working tree clean
- add a temporary file which we do not want to add to git
1
2
3
4
5
6
7
8
9
$ touch temp_file
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
temp_file
nothing added to commit but untracked files present (use "git add" to track)
- Use concept of .gitignore to automatically ignore the temp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ touch .gitignore
$ echo "temp_file" > .gitignore
# now git will automatically ignore the temp_file
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
$ git add .gitignore
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: .gitignore
$ git commit -m "added git ignore file to skip adding of temp files"
[master d692bef] added git ignore file to skip adding of temp files
1 file changed, 1 insertion(+)
create mode 100644 .gitignore
$ git status
On branch master
nothing to commit, working tree clean
- add a new file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ touch b_file
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
b_file
nothing added to commit but untracked files present (use "git add" to track)
$ git add b_file
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: b_file
- add a new file called c_file which we do not want to stage but we will do that to show how to fix the issue
1
2
3
4
5
6
7
8
9
$ touch c_file
$ git add c_file
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: b_file
new file: c_file
- use git reset to remove c_file from staged commits
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ git reset HEAD c_file
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: b_file
Untracked files:
(use "git add <file>..." to include in what will be committed)
c_file
$ git commit -m "added b_file"
[master a05a255] added b_file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b_file
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
c_file
nothing added to commit but untracked files present (use "git add" to track)
- add a new file called c_file which we do not want to stage or commit, but this time we will add and commit do that to show how to fix the issue
1
2
3
4
5
6
7
8
9
$ git add c_file
$ git commit -m "adding c_file to commit by mistake"
[master d65f80c] adding c_file to commit by mistake
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 c_file
$ git status
On branch master
nothing to commit, working tree clean
- Use git log to get the commit hash for the file comitted before c_file, use that to revert to the last “correct” commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ git log
# Use the commit hash that happened before c_file was added and revert to that version
$ git reset --soft a05a25501e0fae3f35d30bb7d3a9ff4bd27d1169
$ git log
# git status will show that c_file is staged but not comitted
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: c_file
- Now unstage c_file so that it is not added to git any more, thereby reversing the git add and commit process.
1
2
3
4
5
6
7
8
9
$ git reset HEAD c_file
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
c_file
nothing added to commit but untracked files present (use "git add" to track)
- Now goto git hub or gitlab and create an empty repository, the repository will show you it URl, copy that and link your local repository to remote repository
1
$ git remote add origin ssh://git@<YOUR_SERVER OR GITHUB>/Rahul.Chandna/git-training.git
- Now push all locally comitted changes to remote repository
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
c_file
nothing added to commit but untracked files present (use "git add" to track)
$ git push -u origin master
Enumerating objects: 14, done.
Counting objects: 100% (14/14), done.
Delta compression using up to 8 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (14/14), 1.16 KiB | 595.00 KiB/s, done.
Total 14 (delta 1), reused 0 (delta 0)
To ssh://<YOUR_SERVER OR GITHUB>/Rahul.Chandna/git-training.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
c_file
nothing added to commit but untracked files present (use "git add" to track)
- Now go back to the remote reposity and add a file there using the web interface
This is to mimick colaborative work where your collegeue could have edited existing files or added a new file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ # Now add a file direclty on remote via browser
$ # Now let us use git fetch to see what all changes have been done on remote so far
$ git fetch
$ # Once done use the below command to see the name of local and remote
$ git branch -a
$ # use the output of above command to see differences between local and remote
$ git diff master..remotes/origin/master
$ # once happy pull changes from remote to local
$ git pull
Updating a05a255..6a71596
Fast-forward
bla | 1 +
1 file changed, 1 insertion(+)
create mode 100644 bla
$ # since all changes are on local you will see running the command again will not show any differences
$ git diff master..remotes/origin/master