Git Tutorial – Part II

Git Clone

git clone <Repository URL>: Downloads a git repository onto the local machine and its entire version history from the remote repository.

C:\Users\Girish\gitpractice>git  clone https://github.com/Girish579/testrepo.git
Cloning into 'testrepo'...
Logon failed, use ctrl+c to cancel basic credential prompt.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.

Git Status

git status: Lists all new or modified files to be committed (Staged, Unstaged, Untracked).

C:\Users\Girish\gitpractice\testrepo>git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

Git Add (Stage) and Git Restore (Unstage)

git add <filename> : Adds the file to the staging area.

git restore –staged <filename>: Unstages the file from the Staging area

C:\Users\Girish\gitpractice\testrepo>git add "home.txt"

C:\Users\Girish\gitpractice\testrepo>git status
On branch newbranch
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   home.txt

C:\Users\Girish\gitpractice\testrepo>git restore --staged "home.txt"

C:\Users\Girish\gitpractice\testrepo>git status
On branch newbranch
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        home.txt

nothing added to commit but untracked files present (use "git add" to track)

Git Commit

git commit -m “[descriptive message]” : Create a new commit from changes added to the staging area.

C:\Users\Girish\gitpractice\testrepo>git commit -m "Home text file added"
[newbranch 2e406cc] Home text file added
 1 file changed, 3 insertions(+)
 create mode 100644 home.txt

Git Branch

git branch -av : List all existing branches

git checkout <branchname> : Checkouts the branch which we provided.

git branch <new-branch-name>: Create a new branch based on your current HEAD

C:\Users\Girish\gitpractice\testrepo>git branch -av
  main                     848a0c0 Initial commit
* newbranch                2e406cc Home text file added
  remotes/origin/HEAD      -> origin/main
  remotes/origin/main      848a0c0 Initial commit
  remotes/origin/newbranch d432f36 new text file added

C:\Users\Girish\gitpractice\testrepo>git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

C:\Users\Girish\gitpractice\testrepo>git branch newbranch

C:\Users\Girish\gitpractice\testrepo>git branch
* main
  newbranch

C:\Users\Girish\gitpractice\testrepo>git checkout newbranch
Switched to branch 'newbranch'

C:\Users\Girish\gitpractice\testrepo>git branch
  main
* newbranch

C:\Users\Girish\gitpractice\testrepo>git status
On branch newbranch
nothing to commit, working tree clean

Git Push

git push <remotebranch> <localnewbranch>: Publish local changes on a remote.

C:\Users\Girish\gitpractice\testrepo>git push origin newbranch
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 329 bytes | 164.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'newbranch' on GitHub by visiting:
remote:      https://github.com/Girish579/testrepo/pull/new/newbranch
remote:
To https://github.com/Girish579/testrepo.git
 * [new branch]      newbranch -> newbranch

Git Pull Request

Pull requests notifies other team members that they have completed a feature on their local feature branch. Once their local feature branch is ready, the developer sends a pull request via their GitHub or Bitbucket accounts. This lets everyone in the project know that they need to review the code and merge it into the master branch.

Step 1:

Step 2:

Step 3:

References:

  • https://docs.github.com/en/github/getting-started-with-github
  • https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request
  • https://git-scm.com/

Learn more about git in the in the next upcoming Blog Articles.

Happy Learning!