Useful Git Commands

Introduction

Git is like a special tool for computer programmers. It helps them keep track of changes they make to their projects, work together with others, and make sure everything stays organized. But, Git can be a bit tricky to learn because there are lots of things you can do with it. Don't worry! We made a cheat sheet to make it easier for you. It doesn't matter if you're just starting to code or if you're really good at it, this cheat sheet will help you become a Git expert.

We'll explain Git in simple terms. We'll show you important commands and tips. You'll learn how to do basic things like saving your work and making different versions of your project. We'll also teach you some advanced tricks for when things get a bit complicated.

By using this cheat sheet, you'll be able to work faster and better with Git. You won't get confused anymore.

How to setup GIT?

Set Your Author Name. Define the name to be associated with your Git commits.

git config --global user.name [name]

Set Your Author Email. Specify the email address for your Git commits.

git config --global user.email [email]

Check Your Git Configuration: View your Git configuration settings.

git config -l

Useful Git Commands

Access Git Documentation. Find helpful guides and documentation within Git.

git help --all
git [command] -help

Initialize a Git Repository. Start a new version-controlled project.

git init

Save Changes to Git. Add and commit changes to your Git repository.

git add .
git commit -m "first commit"

Amend the Most Recent Commit: Modify the last commit with new changes.

git commit --amend

View Commit History. Explore the history of commits in your branch.

git log

View Changes (Detailed): See detailed information about commit history, including file changes.

git log --summary

View Changes (Briefly): Display a simplified overview of commit history.

git log --oneline

Check Repository Status. Determine the current status of your Git repository.

git status

Unstage a File. Remove a file from the staging area.

git reset [file]

Undo the Last Commit. Revert the most recent commit.

git revert HEAD

Rollback an Old Commit: Undo changes from a specific commit and create a new commit.

git revert [commit_id]

Create a New Branch. Establish a new branch for your work.

git branch [branchName]

List Branches. Display a list of all local and remote branches.

git branch -a

Checkout a Branch. Switch to an existing branch.

git checkout [branchName]

Checkout and Create a Branch. Create and switch to a new branch simultaneously.

git checkout -b [newBranch]

Rename a Local Branch: Change the name of a local Git branch.

git branch -m [old branchname] [new branchname]

Merge Branches. Combine changes from one branch into another.

git merge [branchName]

Abort a Conflicting Merge: Cancel an ongoing merge operation due to conflicts.

git merge --abort

Merge a Remote Repo with Your Local Repo: Combine changes from the remote repository's main branch into your local branch.

git merge origin/main

Rebase Branches. Apply changes from one branch onto another.

git rebase [branch]

Delete a Branch. Remove a branch from your repository.

git branch -d [branchName]

Add a Remote Repository. Link your local repository to a remote one.

git remote add origin [url]

See Remote URLs: Display the URLs of remote repositories linked to your Git project.

git remote -v

Push Changes to Remote. Upload local commits to a remote repository.

git push [remoteURL/remoteName] [branch]

Delete a Remote Branch: Remove a branch from the remote repository.

git push origin --delete [branch name]

Clone a Repository. Duplicate an existing repository to your local machine.

git clone [url]

Clone Private Repository: Clone a private repository using SSH authentication.

git clone ssh://[email protected]/[username]/[repository-name].git

Pull Latest Changes. Retrieve and apply the latest changes from a remote repository.

git pull [branchName] [remoteURL/remoteName]

Stash Changes. Temporarily save your changes without committing.

git stash

Stash Changes with a Message. Stash changes with a descriptive message.

git stash save "message"

List Stashes. View a list of your saved stashes.

git stash list

Apply Latest Stash. Reapply the most recent stashed changes.

git stash pop

Apply Specific Stash. Retrieve and apply a specific stashed set of changes.

git stash pop stash@{[index]}

Remove All Stashed Entries: Delete all stashed changes in Git.

git stash clear

Choose a Commit and Apply It to Another Branch: Apply a specific commit from one branch to another

git cherry-pick <commit-hash>

Pick commit change without create new commit: Prevents Git from automatically creating a new commit after applying the changes.

git cherry-pick <commit-hash> --no-commit

Share the Knowledge

If you found this Git cheat sheet helpful, don't keep it to yourself! Share it with your fellow developers and friends who might benefit from mastering Git. Together, we can make version control a breeze for all.

Conclusion

We cover everything from basic stuff like setting your name to more advanced things like rebasing and stashing. We'll make your work easier, help you get more done, and make Git a tool you're not afraid to use.