Skip to content

Git Commmands


Setting Up a Repository

  1. Initialize a new Git repository:

    git init
    

  2. Clone an existing repository:

    git clone https://github.com/user/repo.git
    

Basic Workflow

  1. Check the status of your repository:

    git status
    

  2. Add changes to the staging area:

    git add filename
    # or add all changes
    git add .
    

  3. Commit changes:

    git commit -m "Commit message"
    

Branching and Merging

  1. Create a new branch:

    git branch new-branch
    

  2. Switch to a different branch:

    git checkout new-branch
    

  3. Create and switch to a new branch:

    git checkout -b new-branch
    

  4. Merge a branch into the current branch:

    git merge branch-to-merge
    

Remote Repositories

  1. Add a remote repository:

    git remote add origin https://github.com/user/repo.git
    

  2. Set the remote URL with a token (as per your example):

    git remote set-url origin https://$GITHUB_TOKEN@github.com/yuva19102003/portfolio.git
    

  3. Push changes to a remote repository:

    git push origin main
    

  4. Pull changes from a remote repository:

    git pull origin main
    

  5. Fetch changes from a remote repository (without merging):

    git fetch origin
    

Viewing History

  1. View commit history:

    git log
    

  2. View a summarized commit history (one line per commit):

    git log --oneline
    

Undoing Changes

  1. Unstage a file:

    git reset HEAD filename
    

  2. Revert changes in a file to the last committed state:

    git checkout -- filename
    

  3. Revert a specific commit:

    git revert commit-hash
    

Stashing Changes

  1. Stash changes:

    git stash
    

  2. List stashed changes:

    git stash list
    

  3. Apply stashed changes:

    git stash apply
    

Working with Tags

  1. Create a new tag:

    git tag -a v1.0 -m "Version 1.0"
    

  2. Push tags to the remote repository:

    git push origin --tags
    

Example Workflow

Let's go through a typical workflow using some of these commands:

  1. Clone a repository:

    git clone https://github.com/yuva19102003/portfolio.git
    cd portfolio
    

  2. Create a new branch:

    git checkout -b new-feature
    

  3. Make changes to files and add them to the staging area:

    git add .
    

  4. Commit your changes:

    git commit -m "Add new feature"
    

  5. Push your changes to the remote repository:

    git push origin new-feature
    

  6. Switch back to the main branch:

    git checkout main
    

  7. Merge the new feature branch into the main branch:

    git merge new-feature
    

  8. Push the updated main branch to the remote repository:

    git push origin main
    

This workflow demonstrates how to clone a repository, create and switch branches, make and commit changes, and push those changes to a remote repository.