When I first started developing code the idea of version control was pretty much unknown, especially in the world of web development. As I progressed through my career and worked at different software companies I learned as much as I could and I’ve now settled into the daily routine of committing, branching, pushing and pulling. While this might sound vaguely naughty, if you are writing any sort of code, you should really look into version control. My current favourite is Git.
I’m not going to get into installing and setting up Git. This article is designed merely as a resource of common Git commands, mainly because I tend to forget them 😉 I’ll be adding commands to this list as I need (and discover) them.
If you are on a Mac or Unix type machine you will have a Bash style command line you can use, if you are on Windows I highly recommend using GitBash.
At the moment if you want to use version control and Git, you’re two big choices are GitHub and Bitbucket. I use both, GitHub for public projects and Bitbucket for private projects. Most of the commands listed below will be ones that I use for Bitbucket repositories, but they are also relevant to GitHub. Either or them gives you a step by step guide to creating, initialising and configuring your code folder to work with your repository.
git add –all
This allows you to add all new files to your git repository. It’s not 100% ideal as it might add files you don’t want to necessarily add at this point, but it’s quick and easy.
git commit -a
This command will commit your current changes to the repository and prompt you to enter a message for the commit. I prefer it to using the -m (message) swtich because I can see all the files that have changed for my commit message. If you are using GitBash, you will also need to know how to use Vim style editor commands.
git push -u origin main
This command will push all changes on the current master branch to the remote repository.
git checkout <branch_name>
Switches to the branch ‘branchname’
git checkout -b <branch_name>
Creates and switches to the branch ‘branchname’.
git push -u origin<branch_name>
Push all committed changes to the branch ‘branchame’ to the remote repository.
git push –all
Pushes all branches to the remote repository.
Pulls all changes from the remote repository. Useful once you have merged a pull request.
git push origin –delete <branch_name>
Delete a remote branch
git branch -D <branch_name>
Delete a local branch, which has NOT already been fully merged
git branch -d <branch_name>
Delete a local branch, which has already been fully merged
git branch -m new-name
Rename your local branch to new-name.
git branch -m old-name new-name
Rename a different branch to new-name
git push origin :old-name new-name
Delete the old-name remote branch and push the new-name local branch.
git push origin -u new-name
Reset the upstream branch for the new-name local branch.
git reset –hard [commit-id]
Reset a branch back to a specific commit (by id)
git push -f origin [branch]
Push changes reverted by the above command to the remote version of your local branch
git reset –hard [tag]
git push –force origin main
Force the remote branch (main) to be reset back to a specific tag
git checkout develop
git rebase main
This moves the entire develop branch to begin on the tip of the main branch, effectively incorporating all of the new commits that were included in main.
git checkout feature/branch
git merge develop
Move a commit to the correct branch, for example, committed to the develop branch instead of some feature branch
git checkout develop
git reset –hard HEAD~1
Update ~1 with the amount of commits you want to reset the develop branch by
git reset –hard origin/feature/branch
Reset the local feature/branch with the remote feature/branch, when the local branch has diverged.
git remote prune origin –dry-run
Lists branches that can be deleted/pruned on your local environment (ie merged/deleted on the remote)
git remote prune origin
Prune any merged/deleted branches from your local environment
(credit Fizer Khan)
git branch –merged | egrep -v “(^*|main|dev)” | xargs git branch -d
Delete all local branches that have been merged to
(credit Daine Mawer)
Other cool Git commands from people around the web
Because I always forget how to set up a new or existing git repository
Create a new repository
git clone firstname.lastname@example.org:jonathanbossenger/reponame.git cd reponame touch README.md git add README.md git commit -m "add README" git branch -M main git push -u origin main
Push an existing folder
cd existing_folder git init git remote add origin email@example.com:jonathanbossenger/reponame.git git add . git commit -m "Initial commit" git branch -M main git push -u origin main
Push an existing Git repository
cd existing_repo git remote rename origin old-origin git remote add origin firstname.lastname@example.org:jonathanbossenger/reponame.git git push -u origin --all git push -u origin --tags