Git in 10 commands to do 99% of gitting and keeping out of trouble

Git is hard, probably harder than it needs to be but I have been using it for about 5 years and have a workflow that works for me. I use git from a command line and have learnt how to use these ten commands. If I need to deal with a merge conflict, I use VS Code and the gitlens extension. If I need to do anything else then I probably copy out the files I want to keep, reset my local repo or delete it and clone a new repo then paste back the files I want to include in the change.

Starting a new piece of work

This requires creating a new branch. We want to checkout master, make sure it is up to date and then create a new branch:

git checkout master

 

git pull

 

git checkout -b new_branch_name

Now, if the checkout or the pull fail then I will likely do a git stash to throw away any local changes:

git stash --include-untracked

If I still can’t pull from master it is probably because I have commits that I don’t want on the remote master. I will probably clone a new version of the repo:

rm -rf repo (or mv repo oldrepo)

then

git clone git://url

then I will create the new branch:

git checkout -b new_branch_name

Committing Work

I have done my work and want to commit it, either to then push to the remote repo or to add more work later on.

git add .

I very rarely want to only add a subset of files, if I do then I will put the full path to the file to add:

git add ./folder/folder/folder/file.ext

Once I have added the files I will check what state I am in:

git status

What I am looking for is a) I have remembered to checkout a branch and am not working on master and b) that I have got the files ready to commit that I expect.

If I forgot to create a branch, then I will stash everything, checkout the new branch then pop the changes into the new branch:

git stash --include-untracked
git pull
git checkout -b new_branch_name
git stash pop

  I realise you could just do a git checkout -b but I like to keep it simple and keep my branch up to date.

If I have the correct files in my list of staged files then I will commit the files:

git commit -m "message"

If there are files that I don’t want to check it, maybe I fat fingered a change to a file I didn’t want I will either fix it up manually or checkout the original version of the file:

git checkout -- ./folder/folder/folder/file.ext

and then I will commit the files I do want.

Sharing my changes

When I am ready to push my changes to the remote repo, I do a git push:

git push

This always fails the first time I run it but the output shows the command I do need to run. I copy and paste that output and push my changes.

Red Green

From here, if I need to make any changes I go and do that, add the files, commit that changes and push them until I am done. If there have been changes on master I don’t have then I will bring them locally:

git pull origin master

origin is the name of the remote, if you aren’t sure what it is do:

git remote --v

If you get a merge conflict then open the directory in VS Code. Make sure you have gitlens installed as an extension in VS Code and then use the file explorer to find the files that are a bluey colour. When you have found the files with a conflict, open them and browse to the sections with the conflicts and VS Code will let you either keep the incoming change (the changes already on master), keep your local changes (the changes you just made) or keep both. VS Code does a pretty good job of merging the files.

Once you have fixed the merges then go back to add, commit, push.

Finish

Lastly checkout master and pull down the latest changes:

git checkout master

Then check for any branches that can be deleted and get rid of them. git branch shows all the local branches:

git branch

If you no longer need a branch then:

git branch -D branch_name

and shazam, the branches gone. You are now back at master, ready to start the merry-go-round again.

If you read this, scoffed and went “oh you could have done it like x” well great, I am sure there are loads of efficiencies to make here but this works for me so I am happy with my git choices.

More Help

For help with specific scenarios: https://dangitgit.com/en

(and the ten commands to stop you having to count and proove me wrong):

  • git pull
  • git checkout
  • git stash
  • git add
  • git commit
  • git branch
  • git push
  • git clone
  • git status
  • git remote