Git Workflow: Useful Commands

Useful Everyday Git Commands

17-04-2019 - 2 minutes -
git

This is less of an article and more of a list of useful Git commands I use day-to-day, for personal reference and hopefully to help out others. Most of these are based on working with GitHub.

To get a repo

git clone https://github.com/account/repo.git
git config user.name myusername
git config user.username myusername
git config user.email myusername@noreply.users.github.com

Change 'myusername' to your own username

To update from upstream (sync a fork)

git checkout master
git remote add upstream git://github.com/user/repo.git
git pull upstream master


To create a branch

git checkout basebranch
git pull origin basebranch
git checkout -b newbranch


To commit

git add .
git checkout -m "message"

or if you only want to commit files you have modified, skip the add stage:

git checkout -am "message"


To commit part of a file

git add -p filename

-p is short for --patch
This command brings up an interactive patch menu containing 'hunks', which is what Git thinks are suitable chunks of code
Answer hunks as:
[y]es - add hunk
[n]o - don't add hunk
[s]plit - split hunk into smaller hunks
[e]dit - modify hunk (change +/-/# lines)
[q]uit - quit, aborting all changes


To undo adding files to staging area

git reset HEAD filename


To change a commit message

git commit --amend -m "newmessage"


To view changes

git diff
git dif --staged

Adding --staged (or --cached) views staged changes

To undo and go back 1 commit

git reset --soft HEAD~1
git reset --hard HEAD~

--soft keeps local changes
--hard destroys local changes (careful with this one!)

To view all logs in branch compactly

git --no-pager log --oneline


To merge

git rebase -i commitSHA
git commit --amend
git push origin mergingbranch --force
git checkout basebranch
git pull origin basebranch
git checkout mergingbranch
git rebase master 
git push origin mergingbranch --force 
git checkout master
git merge --no-ff mergingbranch
git pull origin master

The first command, git rebase -i commitSHA, uses the commit hash as the commit to rebase from
An alternative to this command is git rebase -i HEAD~10 where 10 is the number of commits to rebase
git commit --amend changes the commit message (Git by default merges all commit messages from the rebase to make a new one)
Make sure to resolve conflicts and ensure all test builds pass during git rebase master!
Using git merge --no-ff commits without a fast-forward, to maintain a proper commit chronology without any real downsides


To tag for release

git tag -am "release details" 1.3.2
git push origin branchname --tags

Change 1.3.2 to release number, add release details

To change a tag's details

git tag tagname tagname -a -f
git push origin branchname --tags --force


To view nearest tags to current commit

git describe --tags --abbrev=0


To update 'develop' branch with new release

git checkout release-1.0
git pull origin release-1.0
git checkout develop
git pull origin develop
git merge --no-ff release.1.0
git push origin develop