Git Feature Branches

Git Feature Branches

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

Leave a comment