Git Workflow Commands

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 [email protected]

Change 'myusername' to your own username <br><br>

To update from upstream (sync a fork)

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

<br>

To create a branch

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

<br>

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"

<br>

To commit part of a file

git add -p filename

-p is short for --patch<br> This command brings up an interactive patch menu containing 'hunks', which is what Git thinks are suitable chunks of code<br> Answer hunks as:<br> yes - add hunk<br> no - don't add hunk<br> split - split hunk into smaller hunks<br> edit - modify hunk (change +/-/# lines)<br> quit - quit, aborting all changes<br> <br><br>

To undo adding files to staging area

git reset HEAD filename

<br>

To change a commit message

git commit --amend -m "newmessage"

<br>

To view changes

git diff
git dif --staged

Adding --staged (or --cached) views staged changes <br>

To undo and go back 1 commit

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

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

To view all logs in branch compactly

git --no-pager log --oneline

<br>

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

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 <br><br>

To change a tag's details

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

<br>

To view nearest tags to current commit

git describe --tags --abbrev=0

<br>

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