Table of contents
Let's start with a basic and very important command that is used to initiate Git in your machine.
sudo git init
Reinitialized existing Git repository in /prod/DevOpsHit3/.git/
Another command to clone a repository from Remote to your local machine.
git clone [repository URL]
//Example
git clone https://github.com/velosadmin/DevOpsHit3.git
Git Status: git status command is used to check the untrack files which may or have to stage then commit.
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
Test1.txt
nothing added to commit but untracked files present (use "git add" to track)
Git add command: If you want to stage above Test1 file you can use git add command with file name or you have multiple files and want to stage all at a time you can use "git add ."
git add filename/you can use dot(.) if you want to stage all file at a time
Example
git add .
git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: Test1.txt
Now file Test1 is staged but still not committed.
We use the command git commit -m "Message" where -m is a tag which is used to save your message without opening any editor.
git commit -m "my first commit"
[master 158b631] my first commit
1 file changed, 1 insertion(+)
create mode 100644 Test1.txt
//Now I do not have nothing to commit refer below.
git status
On branch master
nothing to commit, working tree clean
Now I want to push the same code/file to the remote from where I pull the code and I want to sync up my code with the remote code.
I will use command git push origin master , here master is the branch
git push origin master
//Below is the output
Username for 'https://github.com': velosadmin
Password for 'https://velosadmin@github.com':
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 344 bytes | 344.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/velosadmin/DevOpsHit3.git
7bda836..158b631 master -> master
Now Let's check on our GitHub if we got file Test1.txt or not.
Yes, we got the file here ,refer below.
Now, I want to check how many branches I have.
$ git branch
main
* master //it means your control is on master branch right now
Now, I want to switch to the branch main with the help of git checkout command.
git checkout main
Switched to branch 'main'
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
//If i do git branch I will be on main branch see below.
git branch
* main
master
Now I want to merge my master branch code to the main branch if you remember there is a file Test.txt and Test1.txt that is not available in the main branch right now and want to merge it to the main branch now.
ls
README.md
$ git merge master
// see the below output
Updating 8a48919..158b631
Fast-forward
Test.txt | 3 +++
Test1.txt | 1 +
2 files changed, 4 insertions(+)
create mode 100644 Test.txt
create mode 100644 Test1.txt
Now let's do ls on the main branch.
git branch
* main
master
ls
README.md Test1.txt Test.txt //we have both files in main branch too
Now, I want to see the logs when the commit was done and other details.
$ git log
// here is complete log what and when we have done
commit 158b6316e74163e71d8f4b62eb28f6b363345e23 (HEAD -> main, origin/master, master)
Author: “velosadmin” <“saifmca2017@gmail.com”>
Date: Sun Apr 9 13:59:42 2023 -0400
my first commit
commit 7bda8363c6e5d1d16b181037d3306e3e4af8b8a4
Author: “velosadmin” <“saifmca2017@gmail.com”>
Date: Sat Apr 8 17:47:11 2023 -0400
Second push
commit 4c50683a517d35559017a70c89524a97630da97a
Author: Saif <saifmca2017@gmail.com>
Date: Sat Apr 8 17:19:43 2023 -0400
Test
commit 8a4891954a9d5f467c12b4d831d4045d5762167f
Author: Saif <saifmca2017@gmail.com>
Date: Sat Apr 8 17:14:06 2023 -0400
est
commit 07940d8940ffa726e70c46347e4b0c8d146b0b86 (origin/main, origin/HEAD)
Author: velosadmin <95381857+velosadmin@users.noreply.github.com>
Date: Sat Apr 8 17:00:27 2023 -0400
Initial commit
Now, I want to see all the remote repositories' URLs.
git remote -v
origin https://github.com/velosadmin/DevOpsHit3.git (fetch)
origin https://github.com/velosadmin/DevOpsHit3.git (push)
Now, I want to create a new branch called dev
git checkout -b dev
Switched to a new branch 'dev'
Now let's discuss what is Git Branching.
What is Git Branching?
Git branching is a technique that in which you can create a new branch and copy the codebase to your branch and do experiments on your code to fix a bug or add a new feature without affecting the code base and you can merge it back to the code base if it is required.
What are Git Merge and Git Rebase?
Git Merge: Git merge is a feature to add your branch one code to branch 2.
For example, I have a branch master in which I have a codebase and I have to pull this code to my Dev branch, and added some more features to this code now I want to add the same feature on the master branch, in this case, I will check out my master branch and used command "git merge dev" and my dev code will be merged into the master branch, it is called git merge.
You can my previous example for the same.
Git Rebase: Git rebase is also used to migrate the code from one branch to another branch but can create a cleaner commit history, but with a higher risk of conflicts.
Now let's take an example or task for better understanding.
Let's create a file in my Dev branch
nana feature01.txt
// add below comment in the file and commit it
This is the bug fix in development branch
1st line>> This is the bug fix in the development branch
Commit this with the message “ Added feature2 in development branch”
2nd line>> This is gadbad code
Commit this with message “ Added feature3 in development branch
3rd line>> This feature will gadbad everything from now.
Commit with message “ Added feature4 in development branch
Here is the my command line inputs.
root@saif-HP-Pavilion-g6-Notebook-PC:/prod/DevOpsHit3# nano feature01.txt
root@saif-HP-Pavilion-g6-Notebook-PC:/prod/DevOpsHit3# ls
feature01.txt README.md Test1.txt Test.txt
root@saif-HP-Pavilion-g6-Notebook-PC:/prod/DevOpsHit3# git add .
root@saif-HP-Pavilion-g6-Notebook-PC:/prod/DevOpsHit3# git commit -m "Added feature2 in development branch"
[dev 162278b] Added feature2 in development branch
1 file changed, 2 insertions(+)
create mode 100644 feature01.txt
root@saif-HP-Pavilion-g6-Notebook-PC:/prod/DevOpsHit3# nano feature01.txt
root@saif-HP-Pavilion-g6-Notebook-PC:/prod/DevOpsHit3# git add .
root@saif-HP-Pavilion-g6-Notebook-PC:/prod/DevOpsHit3# git commit -m "Added feature3 in development branch"
[dev 1fb8602] Added feature3 in development branch
1 file changed, 5 insertions(+)
root@saif-HP-Pavilion-g6-Notebook-PC:/prod/DevOpsHit3# git nano feature01.txt
git: 'nano' is not a git command. See 'git --help'.
The most similar commands are
annotate
daemon
root@saif-HP-Pavilion-g6-Notebook-PC:/prod/DevOpsHit3# nano feature01.txt
root@saif-HP-Pavilion-g6-Notebook-PC:/prod/DevOpsHit3# git add .
root@saif-HP-Pavilion-g6-Notebook-PC:/prod/DevOpsHit3# git add .
root@saif-HP-Pavilion-g6-Notebook-PC:/prod/DevOpsHit3# git commit -m "Added feature4 in development branch"
[dev b88472a] Added feature4 in development branch
1 file changed, 1 insertion(+)
Now I want to go back to my first commit "This is the bug fix in the development branch" just get the commit Id and then use reset with commit id as below.
git reset b88472a
Happy Learning
If you like the such content please follow me and hit the like button
Thanks
Saif Ali,