Introduction to Git [Part 2]

aaronj

Aaron Johnson

Posted on September 28, 2022

Introduction to Git [Part 2]

Disclaimer: I've re-arranged the commands with the 1'st part so as to have a flow with the introduction of commands.

Some Basic Git Commands

1) git status
2) git commit
3) git checkout
4) git push
5) git reset

1)git status - The command is used to display the state of the working directory and the staging area, it also shows which files have been tracked (Added to staging area) and untracked (Not Added to staging area).

2) git commit - The command is used to create a snapshot of all the changes made to the repository and will not be changed unless asked to and this will be "pushed" to the main repository. The syntax of the command is "git commit", if you would like to add a message to the commit, type the command as git commit -m "Enter message here", and if you use the command as is, it will open a message editor where you enter the message for the commit and exit that screen type ":wq".

So this is how it should appear after using the command.


example text
1 file changed, 1 insertion(+)
create mode 100644 text.txt


3) git checkout - The command is used to change between branches in the repository. In this example, I have created a new branch called "demo" using the "git branch branchname" command. So to change the branch, the syntax would be "git checkout branchname", in this case, it would be "git checkout demo" and the following should appear.


git checkout demo
A text.txt
Switched to branch 'demo'


4) git push - The command is used to push all the changes in your repository to the main repository. When you use the command


 push origin <branch_name>

Enter fullscreen mode Exit fullscreen mode

The following should appear.


Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 254 bytes | 254.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To [The URL of your repository]


Note: When you're pushing to a remote repository make sure that the branch from which you're pushing from locally matches the same as the branch you're referring to in the push command, ie main to main or custom_branch to custom_branch.

5) git reset: The command resets your cloned repository or in better terms, undoes the changes you've made to the repository.

For example, I've added a file test4.c to the staging area by mistake and now I want to undo it. Currently, if i use the git status command I will see



On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   test4.c


Enter fullscreen mode Exit fullscreen mode

As clearly shown, the file is staged. To undo this, use the command git reset, and now when you use the git status command, you will see



On branch main
Your branch is up to date with 'origin/main'.

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

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


Enter fullscreen mode Exit fullscreen mode

Now the file is untracked and your staging area is empty.

I hope the post was helpful and as usual, here are a few more sources to learn from.

A few sources to learn the command line

💖 💪 🙅 🚩
aaronj
Aaron Johnson

Posted on September 28, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related