Web Analytics Made Easy - Statcounter

What are the important Git commands used with GitHub workflows?

GitHub, a platform for hosting and collaborating on Git repositories, relies on Git for version control. Below are important Git commands commonly used with GitHub workflows:

Basic Git Commands

  1. Configuration:
    • git config --global user.name "Your Name"
      Sets the username for commits.
    • git config --global user.email "your_email@example.com"
      Sets the email address for commits.
  2. Initialize a Repository:
    • git init
      Creates a new Git repository in the current directory.
  3. Clone a Repository:
    • git clone <repository_url>
      Clones an existing repository to your local machine.
  4. Check Repository Status:
    • git status
      Shows the status of the working directory and staging area.
  5. Add Changes:
    • git add <file_name>
      Stages a specific file for commit.
    • git add .
      Stages all changes in the current directory.
  6. Commit Changes:
    • git commit -m "Commit message"
      Saves staged changes with a message.
  7. Push Changes:
    • git push origin <branch_name>
      Pushes local commits to the remote repository.
  8. Pull Changes:
    • git pull origin <branch_name>
      Fetches and merges changes from the remote branch to the local branch.

Branch Management

  1. Create a Branch:
    • git branch <branch_name>
      Creates a new branch.
    • git checkout -b <branch_name>
      Creates and switches to a new branch.
  2. Switch Branches:
    • git checkout <branch_name>
      Switches to the specified branch.
  3. List Branches:
    • git branch
      Lists all local branches.
    • git branch -r
      Lists all remote branches.
  4. Delete a Branch:
    • git branch -d <branch_name>
      Deletes a branch locally.

Merging and Rebasing

  1. Merge Branches:
    • git merge <branch_name>
      Merges the specified branch into the current branch.
  2. Rebase Branches:
    • git rebase <branch_name>
      Re-applies commits from one branch onto another.

Remote Repository Management

  1. Add a Remote Repository:
    • git remote add origin <repository_url>
      Links a local repository to a remote repository.
  2. List Remote Repositories:
    • git remote -v
      Shows the URLs of remote repositories.
  3. Remove a Remote:
    • git remote remove <name>
      Removes a remote repository.

Inspect and Compare

  1. View Commit History:
    • git log
      Displays commit history.
    • git log --oneline --graph --all
      Shows a compact, graphical view of commit history.
  2. View Differences:
    • git diff
      Shows differences between working directory and staging area.
    • git diff <branch_name>
      Compares the current branch with another branch.
  3. Show Commit Details:
    • git show <commit_hash>
      Displays details about a specific commit.

Stashing and Cleaning

  1. Stash Changes:
    • git stash
      Temporarily saves changes not ready for commit.
    • git stash apply
      Re-applies stashed changes.
    • git stash drop
      Deletes a stash.
  2. Clean Untracked Files:
    • git clean -f
      Removes untracked files.

Tagging

  1. Create a Tag:
    • git tag <tag_name>
      Creates a lightweight tag.
    • git tag -a <tag_name> -m "Message"
      Creates an annotated tag.
  2. Push Tags:
    • git push origin <tag_name>
      Pushes a specific tag.
    • git push origin --tags
      Pushes all tags.
  3. List Tags:
    • git tag
      Lists all tags.

Undo Changes

  1. Unstage Changes:
    • git reset <file_name>
      Unstages a file from the staging area.
  2. Undo Last Commit:
    • git reset --soft HEAD~1
      Keeps changes in the working directory.
    • git reset --hard HEAD~1
      Deletes the last commit and changes.
  3. Revert a Commit:
    • git revert <commit_hash>
      Creates a new commit that undoes the specified commit.

GitHub-Specific Commands

  1. Fork a Repository:
    • Done through the GitHub interface, not via command line.
  2. Open a Pull Request:
    • Use GitHub’s web interface to propose changes to a repository.
  3. Resolve Conflicts:
    • Fetch changes with git pull, resolve conflicts in the code editor, and commit.

Tips for Using Git Commands

  • Use git help <command> for detailed help on any command.
  • Always check the status with git status before committing or pushing.
  • Write meaningful commit messages to maintain a clear history.
  • Use .gitignore to exclude files from being tracked.

These commands form the foundation of GitHub workflows and can handle most version control tasks effectively.


Discover more from Technology with Vivek Johari

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from Technology with Vivek Johari

Subscribe now to keep reading and get access to the full archive.

Continue reading