Skip to main content

Git and Github Commands

Git Commands​

  1. git init​

The git init command is used to initialize a new Git repository in your project folder. This command is used once during the initial setup of a new repository.

  1. git status​

The git status command displays the status of your working project. It shows which files have been modified or staged and which files are untracked.

  1. git add​

The git add <filename> command adds new or changed files in your working project to the Git staging area. You can add specific files by providing their filenames, or you can use git add . to add all changed files.

  1. git commit​

The git commit command is used to record the changes in the repository. Each commit includes a commit message that describes the changes made in that commit.

Syntax :

git commit -m "commit message"
  1. git push​

The git push command is used to upload local branch commits to GitHub. It pushes your local changes to the remote repository on GitHub.

To push your code to GitHub using Git commands, follow these steps:

Pushing Existing Code to a Git Repository​

  1. Initialize a Git repository:​

If you haven't already done so, navigate to the root directory of your project using a command-line interface, and run the following command to initialize a Git repository:

git init
  1. Check git status :​

    This command is used to check the status of git repository like if any nodification done then it will track it.

git status
  1. Add a changes in file​

    By using given commands you can add changes in github repository , changes can be add by two methods :
    1. git add . :- When more than one file is changed , this command is used.
    2. git add <file name> :- When only one file change , this command is used.
git add .
git add <file name>
  1. Commit changes​

    After changing or modifying any file we have to add code , after modifying code we have to add commit for showing what changes in repository
git commit -m "add a message "
  1. Push the changes:​

    By using this command code can be push in github repository after creating repository
git push

After this error will come it will then we have to add github repositary where we have to add the code

  1. Create a repositary :​

    Create repository if dont know how to create repository then click here to watch steps for creating repository

  2. Copy command lines​

link img

  1. Use the following command to connect local repo to github repo​

git remote add origin <repository url>
  1. Use follwing command to push code​

git push
  1. Copy the command from terminal​

git push --set-upstream origin master
  1. After adding this command on terminal your code will be pushed on github Repository​

    Code added in Repositary successfully.

  2. After adding file modified changes can be added by using:​

git add .
git commit -m "changes message"
  1. After adding and commiting changes push the changes​

git push

Then changes will be added successfully.

Pushing New Code Directly to a Git Repository​

  1. Clone the repository:​

    Start by cloning the existing repository to your local machine. Use the following command to clone the repository:
git clone <repository_url>

Replace <repository_url> with the URL of the repository you want to clone.

  1. Create or modify files:​

    Create new code files or modify existing ones using your preferred code editor or IDE.

  2. Add files to the staging area:​

    Use the git add command to add the new or modified files to the staging area:

git add .
  1. Commit the changes:​

    Commit the changes by running the following command:
git commit -m "Commit message"

Replace Commit message with a descriptive message summarizing the changes made.

  1. Push the code:​

    Finally, push the new code to the remote repository using the following command:
git push

Great job! You have now successfully pushed your new code directly to the Git repository.