In modern software development, mastering Git command line commands is essential. Git is a powerful version control system that allows developers to manage their projects efficiently. Whether using Git directly from the command line, Git console, or terminal, understanding its commands is crucial.
The command line interface (CLI) provides a direct and flexible way to interact with Git repositories. For Windows users, familiarity with the Git command line is essential. Additionally, the GitHub CLI simplifies collaboration by enabling users to perform various Git operations directly from the command line.
From basic commands like git commit to more advanced operations, such as branching and merging, proficiency in command line Git enhances productivity and facilitates seamless project management. In this introductory guide, we’ll explore the fundamentals of Git command line usage, empowering developers to streamline their workflows and contribute effectively to collaborative projects.
Git Command Line Commands List
- git diff: Show file differences not yet staged.
- git commit -a -m “commit message”: Commit all tracked changes with a message.
- git status: Show the state of your working directory.
- git add file_path: Add file(s) to the staging area.
- git checkout -b branch_name: Create and switch to a new branch.
- git checkout branch_name: Switch to an existing branch.
- git commit –amend: Modify the last commit.
- git push origin branch_name: Push a branch to a remote.
- git pull: Fetch and merge remote changes.
- git rebase -i: Rebase interactively, rewrite commit history.
- git clone: Create a local copy of a remote repo.
- git merge: Merge branches together.
- git log –stat: Show commit logs with stats.
- git stash: Stash changes for later.
- git stash pop: Apply and remove stashed changes.
- git show commit_id: Show details about a commit.
- git reset HEAD~1: Undo the last commit, preserving changes locally.
- git format-patch -1 commit_id: Create a patch file for a specific commit.
- git apply patch_file_name: Apply changes from a patch file.
- git branch -D branch_name: Delete a branch forcefully.
- git reset: Undo commits by moving branch reference.
- git revert: Undo commits by creating a new commit.
- git cherry-pick commit_id: Apply changes from a specific commit.
- git branch: Lists branches.
- git reset –hard: Resets everything to a previous commit, erasing all uncommitted changes
Important Commands
- git clone https://github.com/username/repository.git Clone a repository from GitHub to your local machine. This command creates a copy of the repository on your local machine.
- git add filename.txt Add changes to the staging area. This command stage changes to the file “filename.txt” for the next commit.
- git commit -m “commit message” Commit changes to the repository with a descriptive message. This command saves changes to the repository with a message explaining the changes.
- git push origin master Push changes from the local repository to the remote repository on GitHub. This command pushes the changes in the local “master” branch to the remote “origin” repository on GitHub.
- git pull origin master Pull the latest changes from the remote repository to the local repository. This command updates the local repository with the latest changes in the remote “origin” repository on GitHub.
- git branch new-feature Manage branches in the repository. This command creates a new branch called “new-feature”.
- git merge new-feature Merge one branch into another. This command merges the changes in the “new-feature” branch into the current branch
𝗘𝘀𝘀𝗲𝗻𝘁𝗶𝗮𝗹 𝗚𝗶𝘁 𝗖𝗼𝗺𝗺𝗮𝗻𝗱𝘀 𝗳𝗼𝗿 𝗔𝘂𝘁𝗼𝗺𝗮𝘁𝗶𝗼𝗻 𝗧𝗲𝘀𝘁𝗲𝗿
- Cloning a Repository
git clone
Clones a remote repository to your local system. - Checking Repository Status
git status
Shows the current status of your working directory, including
modified files. - Checking History & Changes
git log
Displays commit history.
git diff
Compares changes between different commits or branches. - Branching and Switching
git branch
Lists all branches in the repository.
git checkout
Switches to a specific branch.
git checkout -b
Creates a new branch and switches to it. - Staging and Committing Changes
git add
Stages a specific file for commit.
git add.
Stages all changes.
git commit -m “Commit message”
Commits staged changes with a message. - Pushing and Pulling Code
git pull origin
Fetches updates from the remote repository and merges
them into your branch.
git push origin
Pushes local commits to the remote repository. - Merging and Resolving Conflicts
git merge
Merges another branch into the current branch.
git rebase
Moves your changes on top of another branch.
git stash
Saves uncommitted changes temporarily. - Undoing Changes
git reset -hard
Resets to a specific commit, discarding all changes.
git revert
Creates a new commit that undoes changes from a specific
commit.
git checkout –
Discards local changes in a file.
𝗖𝗼𝗺𝗺𝗼𝗻 𝗚𝗶𝘁 𝗘𝗿𝗿𝗼𝗿𝘀 𝗮𝗻𝗱 𝗙𝗶𝘅𝗲𝘀 𝗳𝗼𝗿 𝗧𝗲𝘀𝘁𝗲𝗿𝘀
1. “fatal: Not a git repository”
Cause: Running Git commands outside a Git repository.
Fix: Navigate to a Git repo or run git init to initialize a
repository.
2. “error: Your local changes would be overwritten by merge”
Cause: You have uncommitted changes that conflict with an
incoming merge.
Fix: Commit or stash your changes using git stash.
3. “Merge conflict in <file>”
Cause: Conflicting changes in the same file from different
branches.
Fix: Manually resolve conflicts in the file, then run git add and
git commit.
4. “fatal: Authentication failed”
Cause: Incorrect credentials or expired authentication token.
Fix: Update credentials, re-login, or use a personal access
token.
5. “detached HEAD state”
Cause: Checked out a commit instead of a branch.
Fix: Switch back to a branch with git checkout <branch>.
6. “fatal: origin does not appear to be a git repository”
Cause: The remote repository URL is incorrect or missing.
Fix: Check the remote URL using git remote -v and update if
needed.
7. “‘error: failed to push some refs to <repository>”
Cause: Your branch is behind the remote branch.
Fix: Run git pull -rebase before pushing.
Conclusion:
Mastering Git command line commands is a cornerstone skill for modern developers or Testers, empowering efficient project management and collaboration. With a solid understanding of Git’s command line interface, developers can confidently navigate version control, streamline workflows, and contribute effectively to collaborative projects. However, there may be additional scenarios or insights that could enhance this guide.
I encourage readers to share their comments, suggestions, and any missing scenarios they encounter. By collectively improving and expanding our knowledge of Git command line usage, we can elevate our proficiency and share this valuable expertise with others. Let’s continue to explore, learn, and grow together in our journey with Git.