DevOps Classroom Series – Git – 12/Dec/2019

Git Operations

  • Note: Refer before you start this article.

Untracked file and modified file

  • Execute the following commands. Open Git-Bash or Linux/Mac Terminal
touch 4.txt
echo "Hello" >> 1.txt
  • The above commands create a new file which is not in local repository and modifies a file which is already in local repository
  • Now Execute git status and the following output will be shown
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:   1.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        4.txt

no changes added to commit (use "git add" and/or "git commit -a")

  • Untracked file is a new file which has no history in local repo. Modified file is existing file which has history in local repo.
  • Add only modified file to staging area
git add --help

git add -u

git status
  • Now add the untracked file to staging area and commit the changes to local repo.
git add 4.txt
git commit -m "Third Commit"
git log
git status

Handling multiple Changes

  • Execute the following commands
touch docs/1.txt
touch docs/2.txt
touch test/5.txt
touch test/4.txt
touch 5.txt
touch 6.txt
echo "hello" >> 2.txt
echo "hello" >> test/3.txt
  • If you want add all the changes at once to staging Area
git status
git add -A
# or if you are in top folder on git repo
git add .
git status
git commit -m "Fourth Commit"

ignoring some directories and files

  • Execute the following commands
mkdir bin
touch bin\10.txt
touch 7.txt
echo "hello" >> 6.txt
  • We want everything in bin folder to be ignored. Now execute git status and the following o/p will be shown
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:   6.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        7.txt
        bin/

no changes added to commit (use "git add" and/or "git commit -a")

  • Git has a way to ignore certain files/foders. For that create a file called as .gitignore in the repository root.
touch .gitignore
  • Now add bin/* to the .gitignore file and execute the following
git status # no bin/ information should be shown
git add .
git commit -m "Fifth Commit"
git status
git log

Moving Changes from Staging Area to Working Tree

  • Reset command can help in doing this Preview
  • Execute the following command
touch 8.txt
touch 9.txt
touch 10.txt
echo "hello" >> 7.txt
git status
git add .
git status
  • Now move 10.txt from staging area to working tree
git reset 10.txt
git status
  • Now if you want to remove all the changes in staging area and modified changes in working tree
git reset --hard
git status

  • To remove all the untracked files from working tree
git clean -fd .

Removing files from local repo.

  • Execute the following command
rm 7.txt
  • Execute the following commands
git status
git add .
git commit -m "Sixth Commit"

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner