Useful Git Commands

Useful Git Commands

Some useful command for me to remember.

When working on a new feature switch back to master do a git pull to get the latest and create a new branch.

git branch my-new-feature

Now switch to the new branch.

git checkout my-new-feature

After working on you feature you can push updates. Before pushing run.

git status

This will display what files you have changed make sure you are not pushing something maybe change by linting etc

git add .
git commit -m 'new update'
git push

It will ask you to set the origin.

If you need to pull down an existing remote branch to work on you can first check the remote branches.

git branch -r

You made need to fetch to get the latest branches

git fetch --all

Now to pull your remote branch and track it locally run.

git checkout --track origin/my-new-feature

Now you can work on your branch update etc.

Fixing Merge conflicts

Checkout the master branch.

git checkout master

Need to get the latest changes as that’s what you will be merging.

Get the latest changes

git pull

Checkout your feature branch as this is what you will be merging master into. The feature branch you have merge conflicts on.

git checkout branch

Merge master into your current branch –no-ff == no fast forward https://hackr.io/blog/difference-between-git-merge-and-git-merge-no-ff

git merge master  --no-ff

Now you should see merge conflicts in the vscode git plugin.

Open the files and where the merges are there is a little link you can click accept incoming changes.

This will overwrite your changes with the latest from master.

Re add your changes with the latest code.

git status 
git add .
git commit -m 'fixing conflict'
git push

Stashing

Sometimes you need to stash your work and return to it after you have checkout another branch.

Stash your changes

git stash

You can list your stashes like this.

git stash list

Checkout another branch do whatever you need to do, come back to the current branch and bring back your stashed changes.

Run stash list to see what index your previous stash was at.

git stash list

You probably want to check the stashed changes to do this run.

git stash show -p stash@{1}

Now bring back your changes.

git stash apply 0

Zero is the index of the hash.

stash@{0}: WIP on my-branch: 4d2b4c444 unit tests updated

Cherry Picking

Every now and again you may need to cherry pick some code from one branch into another say you have had a feature branch merged into master and you need to cherry pick it to a previous branch.

Here is a use full overview https://www.atlassian.com/git/tutorials/cherry-pick

First checkout master and make sure you have the latest code with a git pull.

git checkout master
git pull

The checkout the branch you want to cherry pick the code to.

git checkout branch-name
git pull

Now you can run the cherry pick command to pull only your code from your commit into your checked out branch. You need to use the hash of your commit.

git cherry-pick 6daf096d00d087483b2ff3ba99e8aba07400acaa

Now run a git pull to make sure you have the latest.

git pull

Now test your code fully you might have some merge conflicts address these by either accepting incoming changes or both. When you are happy you can commit it to the branch and skip CI as it will have already been approved and tested.

git add .
git commit -m "cherry picking 6d00096 from master"  
git push origin branch-name

Here is the full code.

git checkout master
git pull
git checkout branch-name
git pull 
git cherry-pick 6daf096d00d087483b2ff3ba99e8aba07400acaa
git pull
git add .
git commit -m "cherry picking 6daf096d00d087483b2ff3ba99e8aba07400acaa from master"
git push origin branch-name

Diff

Diff two branches

git diff branch1..branch2

Diff two branches and file

git diff branch1 branch2 -- src/file.ts

Leave a comment