· 4 min read

Mastering Git: Essential Commands for Everyday Development

In this blog, we'll cover the Git commands that are most commonly used in daily work, focusing on basic usage.

Mastering Git: Essential Commands for Everyday Development

Recently, I've found myself forgetting many Git commands when trying to submit code. So, I decided to revisit Git commands, compile a list of the most frequently used ones, and enrich the content for better understanding. This not only reinforces my memory but also serves as a handy reference for future use.

In this blog, we'll cover the Git commands that are most commonly used in daily work, focusing on basic usage. Parameters within [] are optional, and those within <> are placeholders to be replaced with actual names relevant to your project. Note that in actual commands, you don't include the <> symbols. For instance, <origin> represents the remote repository name, and <master>, <next>, etc., represent branch names.

1. Git Configuration

Generating an SSH Key

To securely communicate with remote repositories, generate an SSH key:

ssh-keygen -t rsa -C "yourEmail@example.com"
  • -t rsa: Specifies the type of key to create (RSA).
  • -C: Adds a comment, such as your email.

Configuring Global Username and Email

Set your global username and email for all Git repositories:

git config --global user.name "Your Name"
git config --global user.email "yourEmail@example.com"
  • These settings are used in commit messages to identify the author.

Viewing Configuration Settings

To view your current Git configuration:

git config --list

2. Cloning a Repository

Clone a remote repository to your local machine:

git clone http://yourGitAddress.git
  • This copies the entire repository, including its history.

3. Checking Status

View the status of your working directory and staging area:

git status
  • Shows tracked and untracked files, changes staged for commit, etc.

4. Staging Changes with git add

Adding Specific Files or Directories

Add a single file or directory to the staging area:

git add file_name.txt
git add path/to/directory

Adding All Changes in the Current Directory

Add all changes (excluding ignored files):

git add .

Updating Tracked Files

Stage modifications and deletions of tracked files:

git add -u [path]
  • Omitting [path] applies it to the current directory.

Adding All Changes Including Untracked Files

Stage all changes, including new files:

git add -A [path]

Interactive Staging

Review and stage changes interactively:

git add -i [path]

5. Committing Changes with git commit

Commit staged changes with a descriptive message:

git commit -m "Your commit message"
  • Use present tense and be concise.

Amending the Last Commit

Modify the last commit (e.g., to fix the message):

git commit --amend -m "Updated commit message"

6. Pulling Updates with git pull

Pulling from a Remote Branch to a Local Branch

git pull origin remote-branch:local-branch
  • Fetches remote-branch from origin and merges it into local-branch.

Pulling into the Current Branch

git pull origin master
  • Pulls from origin/master and merges into the current branch.

Resolving Merge Conflicts

If there are conflicts:

  1. Open conflicting files and resolve issues.
  2. Stage the resolved files:git add resolved_file.txt
  3. Continue the merge:git commit

7. Pushing Changes with git push

Pushing Local Branch to Remote Branch

git push origin local-branch:remote-branch
  • Pushes local-branch to origin as remote-branch.

Pushing to a Same-Named Remote Branch

git push origin local-branch
  • If the remote branch doesn't exist, it's created.

Deleting a Remote Branch

git push origin --delete remote-branch

Or:

git push origin :remote-branch

Pushing Current Branch to Remote

git push origin HEAD:remote-branch
  • Pushes current branch to remote-branch on origin.

Setting Upstream Tracking

Set the remote branch as upstream:

git push -u origin local-branch
  • After this, git push will default to origin/local-branch.

Pushing All Local Branches

git push --all origin

8. Branch Management with git branch

Listing Branches

List all local branches:

git branch

List all local and remote branches:

git branch -a

Creating a New Branch

git branch new-branch

Deleting a Local Branch

Delete a branch (force delete with -D if necessary):

git branch -d branch-name

Deleting a Remote Branch

git push origin --delete branch-name

9. Merging Branches with git merge

Merging a Branch into Current Branch

git merge branch-to-merge
  • Merges branch-to-merge into the current branch.

Merging Without Committing

git merge --no-commit branch-to-merge
  • Allows you to review changes before committing.

Merging Multiple Branches

git merge master branch-to-merge

10. Switching Branches and Files with git checkout

Switching to Another Branch

git checkout branch-name

Checking Out a Specific Tag or Commit

git checkout tag-name

Discarding Changes in a File

git checkout -- file_name.txt
  • Reverts file_name.txt to the last committed state.

Creating and Switching to a New Branch

git checkout -b new-branch

11. Resetting Changes with git reset

Unstaging a Staged File

git reset HEAD file_name.txt
  • Moves file_name.txt from staging back to working directory.

Resetting to a Previous Commit

Soft Reset (Keeps Changes Staged)

git reset --soft HEAD~n
  • Moves HEAD back n commits; changes remain staged.

Mixed Reset (Default; Unstages Changes)

git reset [--mixed] HEAD~n
  • Moves HEAD back n commits; changes are unstaged.

Hard Reset (Discards Changes)

git reset --hard HEAD~n
  • Moves HEAD back n commits; changes in working directory are discarded.

Reset to a Specific Commit

git reset --hard commit_id

12. Removing Files with git rm

Deleting a File from Repository and Local Filesystem

git rm file_name.txt

Deleting a Directory

git rm -r path/to/directory

Removing from Repository but Keeping Local Copy

git rm --cached file_name.txt

13. Moving and Renaming with git mv

Moving a File

git mv file.txt new/path/

Renaming a File

git mv old_name.txt new_name.txt

14. Rewriting History with git rebase

Rebasing Current Branch onto Another

git rebase master
  • Reapplies commits from the current branch onto master.

Continuing a Rebase After Conflict Resolution

git rebase --continue

Aborting a Rebase

git rebase --abort

Skipping a Commit During Rebase

git rebase --skip

15. Conclusion

This guide is a compilation of essential Git commands that are frequently used in daily development work. Understanding these commands can significantly improve your workflow and efficiency. If you find any errors or have suggestions for improvement, please let me know so I can make corrections promptly. Let's learn and grow together!

16. Additional Resources

Happy Coding!

Written by

Felix Yu

Full stack developer in Melbourne and founder of Pixdyne. Also Shuhai Yu on GitHub and LinkedIn.

About meCase studiesGitHub

Got something to make?
Write to me.

info@felixyu.net