Handy Git commands
3 Git commands for developers
Three commands for the everyday awkward moments: git stash to switch branches with dirty files, git checkout to pull one file from another branch, and git reset --hard to throw a local branch away.
Git is a must-have for Salesforce developers nowadays. Here are 3 simple but useful commands which help in navigating through common scenarios: switching contexts, reverting changes, and managing local modifications efficiently.
Save local changes and change branch
If you are working on something, but suddenly you need to switch to some other branch, the struggle is real when you don't know the below command.
git stash
git checkout bugfix/urgent-fix
# do some work
# ...
# here you are done and want to switch back
git checkout feature/my-first-feature
git stash pop"git stash" will temporarily save tracked files without committing them as they are still work-in-progress. After some time you can re-apply stashed changes. If you are working on new files be sure to stage them first or use "git stash -u" as it will also support untracked files.
Copy a file from a different branch
Fetch a specific file or folder from another branch into your current working branch. Use it when you need to incorporate changes from file(s) developed in a different branch without merging the entire branch into your current one.
# git checkout <other-branch-name> -- path/to/your/folder
git checkout bugfix/login-fix -- path/to/login/bugfixReset all local changes
Sometimes you end up with a completely devastated local branch and you only want to reset its status to a remote version. Fortunately, some git commands will help you with that.
With the below commands, you will discard local changes in your working directory and reset your branch's state to a remote version of the specified branch (usually the same branch, but on a remote server).
# git reset --hard origin/your-branch
git reset --hard origin/master
git clean -df

