Git Commands: A Comprehensive Cheat Sheet for Developers

Git Commands: A Comprehensive Cheat Sheet for Developers

https://nilebits.com/blog/2024/06/git-commands-cheat-sheet-for-developers/

Git is a vital tool for developers since it makes it possible to collaborate and manage versions of projects of any size. Whatever your level of experience, knowing how to use Git commands will greatly improve your productivity. Numerous Git commands are covered in this cheat sheet, along with code samples for each.

Setting Up Git Commands

1. git config

Configure Git settings, such as username and email.

# Set the username
git config --global user.name "Your Name"

# Set the email
git config --global user.email "[email protected]"        

Repository Management

2. git init

Initialize a new Git repository.

git init        

3. git clone

Clone an existing repository.

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

Basic Commands

4. git status

Check the status of your working directory and staging area.

git status        

5. git add

Stage changes for the next commit.

# Stage a specific file
git add filename

# Stage all changes
git add .        

6. git commit

Commit staged changes to the repository.

# Commit with a message
git commit -m "Commit message"

# Amend the previous commit
git commit --amend -m "Updated commit message"        

7. git push

Push commits to a remote repository.

# Push to the default remote and branch
git push

# Push to a specific remote and branch
git push origin branchname        

8. git pull

Fetch and merge changes from a remote repository.

git pull        

9. git fetch

Fetch changes from a remote repository without merging.

git fetch        

Branching and Merging

10. git branch

List, create, or delete branches.

# List all branches
git branch

# Create a new branch
git branch branchname

# Delete a branch
git branch -d branchname        

11. git checkout

Switch branches or restore working tree files.

# Switch to a branch
git checkout branchname

# Create and switch to a new branch
git checkout -b newbranchname        

12. git merge

Merge changes from one branch into another.

# Merge branch into the current branch
git merge branchname        

13. git rebase

Reapply commits on top of another base tip.

# Rebase the current branch onto another branch
git rebase branchname        

History and Logs

14. git log

View commit history.

# Basic log
git log

# One-line log
git log --oneline

# Graph log
git log --graph --oneline        

15. git diff

Show changes between commits, commit and working tree, etc.

# Diff unstaged changes
git diff

# Diff staged changes
git diff --staged

# Diff between commits
git diff commit1 commit2        

16. git show

Show various types of objects.

# Show a specific commit
git show commit-hash        

Undoing Changes

17. git reset

Reset current HEAD to the specified state.

# Unstage changes
git reset filename

# Reset to a specific commit
git reset --hard commit-hash        

18. git revert

Revert a commit by creating a new commit.

git revert commit-hash        

19. git clean

Remove untracked files from the working directory.

# Show what would be deleted
git clean -n

# Delete untracked files
git clean -f        

Stashing Changes

20. git stash

Stash changes in a dirty working directory.

# Stash changes
git stash

# Apply stashed changes
git stash apply

# List stashes
git stash list

# Drop a stash
git stash drop stash@{0}        

Remote Repositories

21. git remote

Manage set of tracked repositories.

# List remotes
git remote -v

# Add a remote
git remote add origin https://github.com/user/repository.git

# Remove a remote
git remote remove origin        

22. git tag

Create, list, delete, or verify tags.

# List tags
git tag

# Create a tag
git tag v1.0.0

# Delete a tag
git tag -d v1.0.0        

Advanced Commands

23. git bisect

Use binary search to find the commit that introduced a bug.

# Start bisect
git bisect start

# Mark current commit as bad
git bisect bad

# Mark a known good commit
git bisect good commit-hash

# End bisect
git bisect reset        

24. git blame

Show what revision and author last modified each line of a file.

git blame filename        

25. git submodule

Initialize, update, or inspect submodules.

# Add a submodule
git submodule add https://github.com/user/repository.git

# Initialize submodules
git submodule init

# Update submodules
git submodule update        

Conclusion

With this cheat sheet, you may simplify your development routine by getting a thorough knowledge of all Git commands. Learning these commands will help you work with your team more effectively and efficiently manage your codebase, from simple tasks to more complex strategies. As you work on your projects, keep this guide close at hand. Git will quickly establish itself as a vital component of your development toolset.

https://nilebits.com/blog/2024/06/git-commands-cheat-sheet-for-developers/

Jude Ugwuanyi

Lead Graphic Designer

4 个月

Please write about the concept of "stack" and "heap" memory in your blog post. I still don't understand it very well especially in C#. I would be grateful if you do. Have a nice day!

Jude Ugwuanyi

Lead Graphic Designer

5 个月

This is very very helpful. Many thanks for sharing

要查看或添加评论,请登录

Amr Saafan的更多文章

社区洞察

其他会员也浏览了