GitHub
GitHub Basic Commands

Basic GitHub Commands

Here are some of the most important and commonly used GitHub (Git) commands:

  • git init: Initializes a new Git repository.

    git init
  • git clone <repository-url>: Creates a local copy of a remote repository.

    git clone https://github.com/username/repository.git
  • git status: Displays the state of the working directory and staging area.

    git status
  • git add <file>: Stages changes to a specific file.

    git add filename.txt
  • git commit -m "<message>": Commits the staged changes with a descriptive message.

    git commit -m "Initial commit"

Branching and Merging

  • git branch: Lists all local branches in the repository.

    git branch
  • git branch <branch-name>: Creates a new branch.

    git branch new-feature
  • git checkout <branch-name>: Switches to the specified branch.

    git checkout new-feature
  • git checkout -b <branch-name>: Creates and switches to a new branch.

    git checkout -b new-feature
  • git merge <branch-name>: Merges the specified branch into the current branch.

    git merge new-feature

Remote Repository Commands

  • git push origin <branch-name>: Pushes local changes to the remote repository.

    git push origin main
  • git pull: Fetches and merges changes from the remote repository to the local repository.

    git pull origin main
  • git remote add origin <url>: Adds a remote repository.

    git remote add origin https://github.com/username/repository.git

Inspection and Comparison

  • git log: Shows the commit history.

    git log
  • git diff: Displays changes between commits, commit and working tree, etc.

    git diff
  • git show <commit>: Displays information about a specific commit.

    git show abc123

Stashing Changes

  • git stash: Temporarily saves changes in the working directory.

    git stash
  • git stash pop: Applies the most recently stashed changes and removes them from the stash.

    git stash pop

These commands form the foundation of using Git effectively for version control in software development. Mastery of these commands will significantly enhance your workflow and collaboration with other developers[1][2][3][4][6].

1. Set up Git for the first time:

  • Configure your identity (required when committing for the first time):
    git config --global user.name "Your Name"
    git config --global user.email "youremail@example.com"

2. Initialize a Git repository:

  • Initialize a new local repository:
    git init

3. Cloning a repository:

  • Clone an existing repository from GitHub to your local machine:
    git clone <repository-url>

4. Check the status of the repository:

  • View the status of your current repository (staged, unstaged changes):
    git status

5. Staging files:

  • Stage a specific file for commit:
    git add <filename>
  • Stage all files for commit:
    git add .

6. Commit changes:

  • Commit staged changes with a message:
    git commit -m "Your commit message"

7. View commit history:

  • Check the history of commits in the current branch:
    git log

8. Push changes to remote repository:

  • Push your changes to a remote repository (e.g., GitHub):
    git push origin <branch-name>

9. Pull updates from remote repository:

  • Fetch and merge changes from the remote repository to your local repository:
    git pull

10. Create and switch branches:

  • Create a new branch:
    git branch <branch-name>
  • Switch to a different branch:
    git checkout <branch-name>

11. Merge branches:

  • Merge a branch into the current branch:
    git merge <branch-name>

12. View branches:

  • List all branches:
    git branch

13. Delete a branch:

  • Delete a branch locally:
    git branch -d <branch-name>

14. Stash changes:

  • Temporarily save changes that are not ready to be committed:
    git stash
  • Apply stashed changes back to the working directory:
    git stash pop

15. Reverting a commit:

  • Revert a specific commit by commit hash:
    git revert <commit-hash>

These commands cover the essential tasks when working with Git and GitHub, helping you manage versions, collaborate on code, and maintain a clean project history.

Citations: [1] https://github.com/joshnh/Git-Commands (opens in a new tab) [2] https://www.freecodecamp.org/news/10-important-git-commands-that-every-developer-should-know/ (opens in a new tab) [3] https://www.adservio.fr/post/top-20-git-commands-with-examples (opens in a new tab) [4] https://www.syncfusion.com/blogs/post/top-git-commands-for-developers (opens in a new tab) [5] https://education.github.com/git-cheat-sheet-education.pdf (opens in a new tab) [6] https://github.blog/developer-skills/github/top-12-git-commands-every-developer-must-know/ (opens in a new tab) [7] https://gist.github.com/RalucaNicola/98c8a4e89ba901265f5536ec4bc2e419 (opens in a new tab) [8] https://dzone.com/articles/top-20-git-commands-with-examples (opens in a new tab)