Gitub, Uncategorized

Useful Git Commands You Will Use Frequently as a Software Developer

3 min read
Git commands

As a software developer, mastering Git commands is crucial for efficient version control and collaboration. Here’s a detailed list of Git commands you’ll find yourself using frequently:

1. Differences

Shows the differences between the working directory and the staging area. It helps you see what changes have been made to your files that are not yet staged for commit.

git diff

Example:

git diff file_name

This command will show the differences in the specified file.

2. Commit message

Commits all staged changes to the repository with a message describing the changes.

git commit -m "commit message"

Example:

git commit -m "Fixed bug in user authentication"

3. Status of Changes

Displays the state of the working directory and the staging area. It shows which changes have been staged, which haven’t, and which files aren’t being tracked by Git.

git status

4. Add File Path

Adds a file or files to the staging area, preparing them for a commit.

git add file_path

Example:

git add index.html

This command stages index.html for commit.

5. Create New Branch

Creates a new branch and switches to it.

git checkout -b branch_name

Example:

git checkout -b feature/login

6. Switch to Existing Branch

Switches to an existing branch

git checkout branch_name

Example:

git checkout develop

7. Modify Commits

Modifies the most recent commit. This can be used to change the commit message or to add new changes to the last commit.

git commit --amend

Example:

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

8. Git Push

Pushes the specified branch to the remote repository named “origin”.

git push origin branch_name

Example:

git push origin feature/login

9. Git Pull

Fetches changes from the remote repository and merges them into the current branch.

git pull

10. Git Rebase

Rebases interactively, allowing you to edit, squash, or reorder commits.

git rebase -i commit_id

Example:

git rebase -i HEAD~3

This command will start an interactive rebase of the last three commits.

11. Clone Repository

Creates a local copy of a remote repository.

git clone repository_url

Example:

git clone https://github.com/user/repo.git

12. Merge

Merges the specified branch into the current branch.

git merge branch_name

Example:

git merge feature/login

13. Statistics

Shows the commit logs with statistics about the changes.

git log --stat

14. Git Stash

Stashes changes in the working directory so you can work on something else without committing those changes.

git stash

15. Git Stash Pop

Applies the stashed changes and removes them from the stash list.

git stash pop

16. Git show commit_id

Shows details about a specific commit.

git show commit_id

Example:

git show 1a2b3c4d

17. Git reset HEAD~1

Undoes the last commit, preserving the changes locally.

git reset HEAD~1

18. Create a patch file

Creates a patch file for a specific commit.

git format-patch -1 commit_id

Example:

git format-patch -1 1a2b3c4d

19. Apply Patch File

Applies changes from a patch file.

git apply patch_file_name

Example:

git apply fix.patch

20. Delete Branch

Deletes a branch forcefully.

git branch -D branch_name

Example:

git branch -D feature/login

21. Reset

Undoes commits by moving the branch reference to a previous state.

git reset commit_id

Example:

git reset 1a2b3c4d

22. Revert

Undoes changes by creating a new commit that reverses the changes.

git revert commit_id

Example:

git revert 1a2b3c4d

23. Paste Changes

Applies changes from a specific commit to the current branch.

git cherry-pick commit_id

Example:

git cherry-pick 1a2b3c4d

24. Lists all Branches

Lists all branches in the repository.

git branch

25. Reset

Resets everything to a previous commit, erasing all uncommitted changes.

git reset --hard commit_id

Example:

git reset --hard 1a2b3c4d

By incorporating these commands into your workflow, you’ll be able to manage your projects more effectively and collaborate seamlessly with your team.


Leave a Reply

Your email address will not be published. Required fields are marked *