Working with Git commands - IUHEPandML/DataAnalysis GitHub Wiki

Essential Git Commands

Basic Commands

git init                # Initializes a new Git repository in the current directory.
git clone <url>         # Creates a local copy of a remote repository.
git add <file>          # Adds changes in a file to the staging area.
git add .               # Adds all changes in the current directory to the staging area.
git commit -m "<message>"  # Commits changes from the staging area with a message.
git status              # Shows the current state of the working directory and the staging area.
git diff                # Shows the differences between the working directory and the staging area.
git log                 # Shows the commit history.

Branching and Merging

git branch <branch_name>    # Creates a new branch.
git checkout <branch_name>  # Switches to an existing branch.
git branch -d <branch_name> # Deletes a local branch.
git merge <branch_name>     # Merges changes from one branch into the current branch.
git rebase <branch_name>    # Rebases the current branch onto another branch.

Remote Repositories

git remote add <name> <url>  # Adds a remote repository.
git remote remove <name>     # Removes a remote repository.
git remote -v                # Lists all remote repositories.
git fetch <remote>           # Fetches changes from a remote repository.
git pull <remote> <branch>   # Fetches changes and merges them into the current branch.
git push <remote> <branch>   # Pushes changes to a remote repository.

Other Useful Commands

git reset --hard HEAD   # Unstages all changes and discards all uncommitted changes.
git checkout -- <file>  # Discards changes in a specific file.
git stash               # Saves the working directory's state.
git stash pop           # Restores the most recently stashed changes.
git tag <tag_name>      # Creates a tag at the current commit.
git tag -d <tag_name>   # Deletes a tag.

Additional Tips for Newcomers

  • Use Descriptive Commit Messages: Write clear and concise commit messages to describe what changes were made and why.
  • Commit Frequently: Make small, frequent commits. This makes it easier to track changes and revert if needed.
  • Branch Often: Create branches for new features, bug fixes, or experiments. This keeps the main branch stable.
  • Learn Advanced Features: Git has powerful features like rebasing, cherry-picking (applying a single commit from one branch to another), and interactive rebase (reordering and editing commits). These can be very useful for maintaining a clean project history.
  • Use .gitignore: Create a .gitignore file to specify which files or directories Git should ignore. This keeps unnecessary files out of your repository.
  • Review Pull Requests: If working in a team, review pull requests to ensure code quality and consistency.

More Advanced Commands

git rebase -i HEAD~n          # Interactive rebase, allowing you to edit, reorder, and squash commits.
git cherry-pick <commit>      # Apply changes from a specific commit onto your current branch.
git bisect                    # A binary search to find the commit that introduced a bug.
git blame <file>              # Show who made changes to each line of a file and when.

Useful Links:

Also See


Setting Github

⚠️ **GitHub.com Fallback** ⚠️