How to solve - Apache Ftp Client library is printing password on console
The problem comes while using FTPS. When developer uses login method of this…
July 07, 2020
In this guide, we will see git basic commands, and fundamentals of git.
Have a github project, and setup ssh keys
Login to Github.com, and create a test project. Example: git-test
And, setup your ssh keys from Setup Ssh Keys - Github{:target=“_blank”}
Lets first take a look at the different stages in git.
There are four different areas in lifecycle of our code in git.
git add <file>
git commit
to move files here.
(Or, to clone the code)
To get the code or project from Github, goto your project link in Github. Copy the git path, as shown in image:
Open terminal, run command:
git clone https://github.com/goravsingal/git_test.git
It should create a folder with your project name in the current folder. Lets go inside that folder.
Git ls-files
It lists all files tracked by git.
Command git status
tells you the status of your files. It tells in which branch I’m currently. It also tells which are the files which are unstaged.
$ git status
It involves three steps:
git add
)git commit
)git push
)Add a file to staging area
Lets create a test file in the project folder. And, see its status.
$ echo "hi" > test1.txt
# Run git status
$ git status
On branch master
No commits yet
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)
The files you just added will be treated as Untracked files. To add this file to staging area, run command:
$ git add test1.txt
# Run git status
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test1.txt
Add file to repository area (Just before going live)
Now our file is in staging area. Lets promote it to next level by committing it.
$ git commit -m "Commit message"
# Run git status
$ git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.
(use "git branch --unset-upstream" to fixup)
nothing to commit, working tree clean
You should specify very nice commit message. This will help you and your team in future about why this commit happened.
Push file to go live
Push this file to master branch.
$ git push origin master
# run git status
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
You can verify this file by going to your github url in browser.
Example adding two commits in a single push
Lets add one file first in first commit:
# Adds one file
$ echo "hi" > t1
$ git add t1
$ git commit -m "t1"
# Adds second file
$ echo "hi" > t2
$ git add t2
$ git commit -m "t2"
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
# note the "2 commits"
# push changes
$ git push origin master
In above example, we added two files in two separate commits. And, did a single push for both commits. If you look at commit history in your github url, you will find these two separate commits.
Revert file from Staging area
Consider you have created a new file or modified existing file. And, used git add
to add the file to staging area. But, later you want that file to be removed from staging area.
The important point is that we want file to be removed from staging area only, and the chanegs will be intact in the local file.
# run git status after adding file name t3
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: t3
Note, git status shows you the command to revert from stage. There are actually two ways to do this:
# 1
git reset HEAD <file>
# 2
git restore --staged <file>
Both does the same job. Your local file will still has the changes.
Revert local file changes and make it similar to what is live
You have done some changes to files. And, now want to revert that changes so that it become exactly same as what is at github.
$ git checkout -- <file>
# in our example, if we modified file named: t2
$ git checkout -- t2
Method-1
use git command to rename file. (Not through system command)
git mv <old filename> <new filename>
# In my example, rename test2.txt to test3.txt
$ git mv test2.txt test3.txt
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: test2.txt -> test3.txt
You don’t need to rename file on disk. Git command will do it for you.
Method-2
Rename it via system command.
$ mv test2.txt test3.txt
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
deleted: test2.txt
Untracked files:
test3.txt
In this case, git sees this as two separate operations. Delete and add the file. Lets see the magical command.
$ git add -A
It recursively add new changes. It will also check if any file has been added, renamed, moved, deleted.
$ git status
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: test2.txt -> test3.txt
Now, you can commit the changes by git commit, git push
commands.
Reverting file rename
There is a simpler way to do this apart from git reset
.
Just do:
$ git mv test3.txt test2.txt
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
delete a file using git rm
$ git rm <Filename>
# example
$ git rm t2
rm 't2'
$ git status
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: t2
If you do an ls, you will not find that file in local folder. In git status
, you will see that git has staged this change. But the change has not been committed and pushed yet.
Delete file by system command
If we delete file by system command, rm <filename
Run a git status
$ git status
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: test2.txt
It detects the change, that this is not staged. To add this in stage area:
# Below command will add change for any file added/removed/updated
$ git add -A
How to revert deleted file from stage area back to local area
After marking a file for deletion, it will not be visible from local folder. You may want to bring it back from staging area.
git reset HEAD <filename>
$ git reset HEAD t2
Unstaged changes after reset:
D t2
Note, you need to type the filename correctly. Since, it is not in your local folder. And, even after running above command, if you see the file is not in your folder yet.
$ git status
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: t2
no changes added to commit (use "git add" and/or "git commit -a")
Finally, to bring it back.
$ git checkout -- t2
$ git status
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
There can be several files that you do not want to commit in git. Commands like git add -A
recursively add all the files in your folder.
For example, there is a credential files placed in your folder that you do not want to commit. You might accidently commit it.
So, to save from these scenarios. There is a special file called .gitignore
Create this file under your project folder, and you can use file/folder name, pattern. Once you save this file, git will never commit those matched files and folders.
# example file .gitignore
It is important to note that you need to commit this file in git.
Please read continuous posts:
The problem comes while using FTPS. When developer uses login method of this…
While dealing with ELastic Search documents, you faced this issue while updating…
Introduction Drupal provides a powerful comment module, which comes as a part of…
You might need to put sudo before above command. The command will show details…
So, here we are using input variable String[] args without any validation…
Note: I have public URLs of these images, which I want to save. return…
In this post, we will see some of the frequently used concepts/vocabulary in…
System design interview is pretty common these days, specially if you are having…
Introduction You are given an array of integers with size N, and a number K…
Graph Topological Sorting This is a well known problem in graph world…
Problem Statement Given a Binary tree, print out nodes in level order traversal…
Problem Statement Given an array nums of n integers and an integer target, are…