Git Commands

ยท

4 min read

Git Commands
  1. Initialize a New Git Repository:

    To start tracking changes in a project, navigate to the project's root directory and run:

     git init
    
  2. Clone a Repository: To make a copy of an existing Git repository from a remote location (e.g., GitHub), use:

     git clone <repository_url>
    
  3. Configure Git: Set your name and email address, which are used to identify your commits:

     git config --global user.name "Your Name"
     git config --global user.email "youremail@example.com"
    
  4. Check the Status of Your Repository: To see the current status of your repository and understand which files have been modified and staged, use:

     git status
    

    Output:

     On branch main
     Your branch is up to date with 'origin/main'.
    
     Changes not staged for commit:
     (use "git add <file>..." to update what will be committed)
     (use "git restore <file>..." to discard changes in the working directory)
     modified:   file1.txt
     modified:   file2.txt
    
     Untracked files:
     (use "git add <file>..." to include in what will be committed)
     newfile.txt
    
     no changes added to commit (use "git add" and/or "git commit -a")
    
  5. Add Changes to the Staging Area: Stage changes for commit using:

     git add <file1> <file2> ...
    

    To stage all changes, use:

     git add .
    
  6. Commit Changes: The git commit command in Git is used to create a new commit in your Git repository. It records changes, creates a unique identifier, saves a commit message, and more. Use it as follows:

     git commit -m "Your commit message here"
    

    The -m stands for "message," and you follow it with a meaningful commit message enclosed in double quotes.

  7. View Commit History: View a list of commits in the repository:

     git log
    
  8. Create a Branch: To create a new branch for working on a feature or bug fix, use:

     git branch <branch_name>
    
  9. Switch Branches: Change to a different branch using:

     git checkout <branch_name>
    
  10. List All Branches: List all local branches:

    git branch
    

    To see remote branches:

    git branch -r
    

    To see all local and remote branches:

    git branch -a
    

    To create and move to a new branch:

    git checkout -b my-branch-name
    
  11. Merge Branches: Merge changes from one branch into another:

    git checkout <target_branch>
    git merge <source_branch>
    
  12. Pull Changes from a Remote Repository: To fetch the latest changes from a remote repository and merge them into the current branch, use:

    git pull <remote> <branch>
    
  13. Push Changes to a Remote Repository: Push your local changes to a remote repository:

    git push origin <branch_name>
    
  14. Create Tags: Create annotated (version) tags for specific commits:

    git tag -a v1.0 -m "Version 1.0"
    
  15. Git Restore: Use "git restore --staged ..." to discard changes in the working directory. Suggests using git restore to discard changes in specific files in the working directory.

  16. Git Stash: The git stash command is used to temporarily save and set aside uncommitted changes in your working directory. Here are common stash commands:

  • Stash Changes:

      git stash
    
  • List Stashes:

      git stash list
    
  • Apply a Stash:

      git stash apply
    
  • Apply and Remove a Stash:

      git stash pop
    
  • Apply a Specific Stash:

      git stash apply stash@{1}
    
  • Drop a Stash:

      git stash drop stash@{1}
    
  • Clear All Stashes:

      git stash clear
    
  • Save Stash with a Description:

      git stash save "Description of the stash"
    
  • Create a New Branch from a Stash:

      git stash branch new-branch-name stash@{1}
    
  1. Git Remote Add: The git remote add command is used to establish a connection between your local Git repository and a remote repository. Use it as follows:

     git remote add origin <remote_repository_url>
    
  2. Git Push: The git push command is used to upload your local branch's commits and changes to a remote repository. Use it like this:

     git push <remote> <branch>
    

For example:

git push origin main
  1. Extra: Git has many more features and commands, so refer to the official Git documentation or use git --help for more information on specific commands.
ย