Skip to content

GitHub

Git Commands Every Developer Should Know

Version control is crucial in software development, but many engineers often struggle with using Git effectively when they start out. In this guide, we will go over the essential Git commands that you will use on a day-to-day basis. We will also cover common pitfalls that junior Git users encounter and provide tips on how to avoid them. Let’s dive in and learn how to use Git more cleanly, keeping your team happy.

#Pushing Code Changes

#Status

Git constantly tracks the changes you make locally. To understand the current state of your repository, use the following command:

git status

The git status command is essential because it informs your next steps, such as add and commit. It shows the files that Git detects as modified or newly created in your project.

#Diff

To view the exact code changes that Git is aware of and what a commit will look like, use the git diff command:

git diff FILE_PATH

This command displays the added and deleted lines for the specified file. If the diff doesn’t match your expectations, make sure to save your changes in your preferred IDE or text editor. Alternatively, you can use git diff without specifying a file path to see all the changes you have made so far.

#Add

The git status command categorizes files in your local repository as either tracked or untracked. Tracked files are those that Git is aware of and have likely been committed before. Untracked files are new files that you haven’t added yet. The add command informs Git that you want to include a file in a future commit and eventually push it to the remote repository.

Note: Avoid using git add . as it adds all detected changes, including potentially unwanted files. Instead, selectively add individual files or use a proper .gitignore file. For example:

git add <file1> <file2>

By adding only specific files, you ensure that you commit and push the intended changes without including unnecessary code.

#Commit

The commit command is used to create a new commit with the changes you have made.

git commit -m 'YOUR_COMMIT_MESSAGE'

#Push

To send your committed changes to a remote repository (typically GitHub), use the git push command:

git push

By default, this command pushes changes to the remote repository specified by the name origin. It also pushes local branches that don’t have matching counterparts on the remote repository. If you need to push a specific branch to a different remote or branch, use the following syntax:

git push REMOTE_NAME BRANCH_NAME

For example, to push a local branch named my_local_branch_name to a remote branch named remote_branch_name on a remote named remote_name, use:

git push remote_name my_local_branch_name:remote_branch_name

#Dealing with Other People’s Code

#Fetch

The fetch command retrieves new code from a remote repository without automatically merging it with your local code. This gives you full control over how you integrate the new code with your own. Unlike git pull, which combines the fetch and merge operations, git fetch allows you to review and modify the fetched code before merging. To fetch from a remote repository, use:

git fetch REMOTE_NAME

By default, git fetch fetches from the origin remote. To fetch from a different remote, specify the remote name.

#Cherry Pick

Sometimes you may want to incorporate a specific commit into your local branch, even if there is no existing branch to merge or rebase against. In such cases, you can use the cherry-pick command. To cherry pick a commit, you need its unique commit ID. For example, to apply the changes from the commit with the ID COMMIT_ID, use:

git cherry-pick COMMIT_ID

This command allows you to add a specific commit’s changes to your local branch, enabling you to test or work with those changes individually.

#Stash

The stash command is useful when you need to temporarily hide your work in progress, especially when merging code. When you have uncommitted changes, Git becomes cautious, as it doesn’t know how to combine your changeswith the incoming changes. To stash your changes and revert your working directory to a clean state, use:

git stash

Later, when you’re ready to continue working on your changes, you can apply the stash using:

git stash apply

This allows you to switch between tasks or branches without committing incomplete work.

#Working with Branches

#Branch

Branches are a fundamental aspect of Git. They allow you to work on different features or bug fixes independently. To create a new branch, use the following command:

git branch BRANCH_NAME

This command creates a new branch based on your current branch. To switch to the newly created branch, use:

git checkout BRANCH_NAME

Alternatively, you can use a single command to create and switch to a new branch:

git checkout -b BRANCH_NAME

Branching enables parallel development and collaboration within your team.

#Merge

Merging combines changes from one branch into another. To merge a branch into your current branch, use:

git merge BRANCH_NAME

This command brings in the changes from the specified branch and incorporates them into your current branch.

#Rebase

Rebasing is an alternative to merging that allows you to incorporate changes from one branch onto another branch more smoothly. It ensures a cleaner and linear commit history. To rebase your current branch onto another branch, use:

git rebase BRANCH_NAME

This command moves your current branch’s commits to the tip of the specified branch, applying them one by one. Rebasing is particularly useful when you want to keep your branch up to date with the latest changes from a shared branch.

#Managing Remotes

#Remote

The remote command allows you to manage the remote repositories associated with your local repository. To view the remote repositories linked to your project, use:

git remote -v

This command lists all the remotes, their URLs, and the fetch and push URLs separately.

#Adding a Remote

To add a new remote repository, use the following command:

git remote add REMOTE_NAME REMOTE_URL

Replace REMOTE_NAME with the desired name for the remote and REMOTE_URL with the URL of the remote repository.

#Removing a Remote

If you no longer need to interact with a remote repository, you can remove it using:

git remote remove REMOTE_NAME

This command removes the specified remote from your local repository’s configuration.

#Conclusion

Mastering Git and its essential commands is crucial for effective version control and collaboration in software development. By understanding these commands and best practices, you can streamline your workflow, avoid common pitfalls, and ensure a clean and well-organized codebase. Remember to always refer to the official Git documentation for detailed information on each command and explore additional Git features to further enhance your productivity. Happy coding!


Last updated: Aug 23, 2023