DEV Community

SHIVANSH PATEL
SHIVANSH PATEL

Posted on • Updated on

Important Git Commands which every developer needs in his day to day life

Image description

Hi everyone this are the git commands which I use frequently as a developer in my day to day life.so I thought to share this with all of you.please do comment your frequently use git commands in comment section

stash

The git stash command is a utility that allows a developer to temporarily save modified files. However, it is a utility that should only be used occasionally because it only involves local repository history. Code stored in a local stash is not visible to other developers, nor is it replicated to other git repositories like GitHub or GitLab when a push occurs.

git stash push
Enter fullscreen mode Exit fullscreen mode
  • Creates a new stash and rolls back the state of all modified files
git stash apply
Enter fullscreen mode Exit fullscreen mode
  • Takes the files in a stash and places them back into the development workspace, but does not delete the stash from history
git stash pop
Enter fullscreen mode Exit fullscreen mode
  • Takes the files in a stash, places them back into the development workspace and deletes the stash from history

Creating a new branch from existing branch

git checkout -b new-branch-name
Enter fullscreen mode Exit fullscreen mode

Renaming a branch

1.For renaming a branch you have to checkout to that branch you have to rename

git branch -m new-branch-name
Enter fullscreen mode Exit fullscreen mode

2.Delete the old branch name by running

git push origin --delete old-branch-name
Enter fullscreen mode Exit fullscreen mode

3.Reset the upstream branch to the name of your new local branch by running

git push origin -u new-branch-name.
Enter fullscreen mode Exit fullscreen mode

Deleting a branch

For deleting a branch you have to checkout to another branch because git will not let you delete the branch you are currently working on

  • deleting branch locally
git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • deleting a branch Remotely
git push origin --delete <branch-name>
Enter fullscreen mode Exit fullscreen mode

Undo Local Staged Changes in Git

git restore --staged filename
Enter fullscreen mode Exit fullscreen mode

Removing the Last Commit

To remove the last commit from git, you can simply run

 git reset --hard HEAD^ 
Enter fullscreen mode Exit fullscreen mode

If you are removing multiple commits from the top, you can run

git reset --hard HEAD~2
Enter fullscreen mode Exit fullscreen mode

to remove the last two commits. You can increase the number to remove even more commits.

Removing a specific commit

To undo that specific commit, you have to use the commit hash of that specific commit

git revert cc3bbf7 --no-edit
Enter fullscreen mode Exit fullscreen mode

The command above will undo the changes by creating a new commit and reverting that file to its previous state, as if it never changed.

Lastly, use git push to push the change to the remote branch.

Renaming a commit

To change the message of the most recent commit that has not been pushed to the remote repository, commit it again using the --amend flag.

git commit --amend -m"new commit messsage"
Enter fullscreen mode Exit fullscreen mode

Thank you for reading this article

Top comments (4)

Collapse
 
styren profile image
Buster Styren

git stash push can be shortened to just writing git stash. Personally, the most useful git command I know is git add -p to interactively stage hunks.

Collapse
 
imshivanshpatel profile image
SHIVANSH PATEL

Thanks for sharing

Collapse
 
imshivanshpatel profile image
SHIVANSH PATEL

Any one knows how to add syntax highlighting in git commands in markdown 🤔

Collapse
 
nandokferrari profile image
Fernando Ferrari

Use a package like Syntax Highlighter