In this post, we will explore, experiment and see git basic files and folder-related commands if you are familiar with the Linux file system this will be easy for you! But, again no need to worry about that we will see every command execution with experiments.
Git File lifecycle status changes with commands |
Git Status
Git Status will always compare the files and folders with the indexed with untracked files and display their status.
Syntax:
git status [options]
This command will check the status of the current branch by comparing it with the master branch
Example:
git status -s
This `git status` command will show the working tree status. and it is having multiple useful options.
When you use the -s or --short option it will display the concisely where status will be represented with single alphabet
- ' ' = unmodified
- M = modified
- A = added
- D = deleted
- R = renamed
- C = copied
- U = updated but unmerged
In the help page you can find more details about different format output meanings in three different tables.
Add a file to the staging/index area
Adding files and folders to the Stage area
We can add files from the work area to the stage area using the `git add` command.git add command |
Here we can add TEXT files because all program codes will be ASCII text format only.
Syntax:
git add [file-name]this files can be recently modified shell or python program using some editor.
Add a file to the staging/index area
Example:
git status # shows nothing vi public/product.html # create a file and edit then save it. git status # observer it shows red color output. git add public/product.html git status # shows the changes in green
Adding file to Git repository (local) |
Adding entire folder
You are working on a website project you need to add all the images files (.jpg) required for your website are need to place into a folder called 'images'. To send from Untracked state files to tracked level or you can say this as work-copy to index area entire folder you can send by providing foldername as argument to git add command.
clear cd source_code mkdir images css publish cd images #copy some images to images directory here for learning used touch filename.jpg used cd .. git add images
Git add entire folder example |
When you observer the the staged files now the two image files will be in green colored that indicates that they are moved to index area of Git repo.
After adding folder status |
Adding Current folder content
Similar to Unix/Linux uses dot to indicate current folder content it may contain
Git add with current directly |
Syntax:
git add -A
Adding all folders all files -A
add all new and changed files to the staging area
Git add All Folder |
Example:
git add -A
Removing files and directories from repository
Now this can be in three use cases.Use case 1: let's see how to remove a file or directory from local repository and also from the filesystem
git rm testcode.file [code-dir] -f
Remove files from git repository |
git rm publish/product.html # Errors out git rm -f publish/product.html git status cd publish; ls # file removed from the filesystem as wellUse case 2: removing a file or directory from local repo and not from filesystem
git rm testcode.file [code-dir] --cachedUse case 3: removing a file or directory display a trail run, not really deletes anything from the repository.
git rm testcode.file [code-dir] –dry-run
Check the list of files changed
There are lots of options to check this with git and subcommands Here we will see with ls-files option so that you can see the list of files or folders recently changedgit ls-filesList of files in single line space separated
git ls-files –zThis is another wonder option with -d , where Git will lists the files or folders which are last delated from the existing repository.
git ls-files -d
Renaming file using git mv
Similar to LInux mv command we can rename a file or folder using git mv command. Here we can have existing file or folder and renamed to new file name or folder. After this if we check the status in short it will shows 'R' that means Renamed.git mv publish/index.html publish/sanjay.html git status -s -bHope you enjoyed this post of learning love on GitOps!!! Yom be interested in the next git learning post on git branching.
No comments:
Post a Comment