Home ยป Basic GIT Commands For Beginners With Project Set-Up

Basic GIT Commands For Beginners With Project Set-Up

Last updated on May 27, 2022 by

Sometimes, the Team or Developer is assigned a task where they have to use the GIT Commands concept. They have deadlines, so they can’t afford to waste time. Luckily, You are at the right place. Here, we will teach you quick Basic GIT commands for beginners by which you can easily start your work within 10 minutes. Ready? Let’s get started!

Table of Contents
1. git init
2. git status
3. git add
4. git commit
5. git push
6. git branch
7. git checkout
8. Delete a Git Branch
9. How to Delete a Local Branch
10. How to Delete a Remote Branch

01 git init

git init will create a new local GIT repository.

Syntax: git nit

$ git init
Initialized empty Git repository in D:/wamp64/www/example-project/.git/

Normally, the .git directory will be hidden in your machine, you can see it by changing the setting as per your machine.

02 git status

git status generates a list of modified files so that you can easily know which files/directory needs to commit and push to GITHUB.

Syntax: git status

$ git status
On branch master

No commits yet

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

        assets/
        index.html

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

In the above example, git status lists out index.html and assets directory as untracked files, which means that both files are not yet committed or pushed. Let’s add them by using the git add command to track and then commit it.

03 git add

git add is used to add files to the track. Simply, It is used to add your updated files to the current branch.

Syntax: git add <filename with fullpath>

$ git add assets/

$ git add index.html

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   assets/css/style.css
        new file:   assets/js/global.js
        new file:   index.html

In the above example, We have added 2 files by using the git add command. Then we ran again, the git status command to verify the status of that file, It shows files are on track and ready for commit under the Changes to be committed: sentence. Are you getting it? Great! Let’s go ahead.

Did you realize a thing that you need to add files manually one by one using git add <filename>? No? You disappointed us. No problem! Let us show you the easiest way to add all files on track by using just single command.

Syntax: git add .(dot/period)

$ git add .

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   assets/css/style.css
        new file:   assets/js/global.js
        new file:   index.html

git add . will add all the modified files on the track.

04 git commit

git commit is used to save your changes to the local repository. It just records your changes not upload them over the GITHUB.

Syntax: git commit -m "Description or Message explaining what changes have been made for"

$ git commit -m "Initial commit"
[master (root-commit) bc27c57] Initial commit
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 assets/css/style.css
 create mode 100644 assets/js/global.js
 create mode 100644 index.html

In the above example, we have added the commit message “Initial commit” and our changes have been saved to the repository.

Now, It’s time to push(upload) those changes to the GITHUB repository. Hold on! Which repository? Ahhh! We have not created any repository yet. Did we? Let’s quickly create a repository on GITHUB.

  1. Open github.com and sign up and verify your email.
  2. After, the email verification, it will take you to the “Create a new repository” page. If not, then you can find and click the button “Create Repository” or “New” or “Start Project”.
Create new repository in basic git commands
Create a new repository on GitHub
  1. Provide a meaningful name and description for your repository.
  2. For beginners, please choose the public because the private repository requires more setup to push the commits. Now, Click on the “Create Repository” button. It will take you to another screen. On that screen, you can see various commands. Some of them we have already run. So we need to run the remaining. Try to understand those commands and then continue here.
  3. Now, we need to run the remaining commands. So you just need to copy git remote add origin <your repository link> and git push -u origin master commands and run it in CLI like below.
$ git remote add origin https://github.com/example-test/example-test.git
$ git push -u origin master
  1. Then, go to the GITHUB and refresh the page, you can see your work uploaded on the GITHUB. Hurray! Let us tell you you’ve done a very nice job so far.

Important Note: Follow this note if you are facing the issue of “remote: Repository not found.” This error occurred when your git configuration does not match with the remote repository. It is possible that your machine has been used by former developers or teammates and he/she has its own settings. Let’s change the settings now.

1. git config user.name and git config user.email will show you the current user’s name and email.

2. Now, match those settings with your recently created GITHUB account. If you found it mismatched then set it like the following and you are set to run git push -u origin master command again.

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

05 git push

git push is used to publish or upload your changes to your GITHUB repository. You can check all the commits on GITHUB.

Syntax: git push origin<branch_name>

$ git push origin master

Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 417 bytes | 139.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To https://github.com/example-user-demo/example-project.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

github commit tab
See the list of all your commits on GITHUB

06 git branch

git branch is used to show a list of all the branches. The asterisk (*) sign before the branch name denotes your current branch. You can also check all your branches on GITHUB by clicking the branch link, you can find that link right after the commit link above the screenshot.

Syntax: git branch

$ git branch
* master

In the above example, the git branch shows only a “master” branch.

07 git checkout

git checkout serves two purposes. You can create the new branch by adding the -b flag in git checkout and you can also switch the branches. Simply, pass the -b flag to create & switch a branch and if you don’t pass it then it will only switch the branch.

Syntax: git checkout -b<branch_name> // To create & switch to new branch

Syntax: git checkout<branch_name> // To switch the branch

$ git checkout -b test_branch
Switched to a new branch 'test_branch'

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$ git branch
* master
  test_branch

In the above example, we have created a new branch “test_branch’. Then we ran another command to switch back to the “master” branch. To verify the new branch, we ran the git branch command and it lists out the new branch. You can see Asterisk (*) before “master” which means we are on the master branch.

08 Delete a Git Branch

GIT will not allow you to delete the branch you are currently on so you must make sure to checkout a branch that you are NOT deleting.

How to Delete a Local Branch

git branch with -d flag command is used to delete a branch locally.

Syntax: git branch -d <branch_name>

$ git branch 
* master
  test_branch

$ git branch -d test_branch
Deleted branch test_branch (was 92fb9ae).

$ git branch
* master

In the above example, To verify the deleted branch we ran the git branch command. It shows that the branch gets deleted.

How to Delete a Remote Branch

git push origin with -d flag command is used to delete a branch remotely. You can create a new branch and push it so that you can test the command.

Syntax: git push origin -d <branch_name>

$ git push origin -d test_branch
To https://github.com/example-user-demo/example-project.git
 - [deleted]         test_branch


That’s it from our end. We hope this article helped you to learn basic git commands for beginners with Project Set up.

Additionally, read our guide:

  1. How To Delete A Git Branch Both Locally And Remotely
  2. How To Rename Git Branch Both Locally And Remotely
  3. How To Undo Last Commit In Git
  4. How To Undo Add File In Git Before Commit
  5. Difference Between == vs === in JavaScript
  6. How To Remove A Specific Item From An Array In JavaScript
  7. How To Check Array Contains A Value In JavaScript
  8. How To Merge Objects In Vue

Please let us know in the comments if everything worked as expected, if your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post ๐Ÿ™‚ Keep Smiling! Happy Coding!

 
 

Leave a Comment