Fresh Blurbs

Git Utilities - Files That Will Be Pushed and Feature Branch Creation

It's trivial in Git to see what files haven't yet been added or have changes that haven't been committed yet (git status). However, once you commit the changes – there's no easy command to see all the files that would be pushed to the remote repository if you ran "git push"… or: there wasn't one, until now.

The small bash function below does exactly that: shows all the files that are commited and will be pushed if you run "git push". Just put following code in ~/.bash_profile or ~/.profile in your environment:

function gitsanity() {
  currbranch=$(git branch | grep "*" | awk '{ print $2 }')
  git log origin/$currbranch.. --stat
}

and when you type "gitsanity" in a git workspace you should get an output similar to:

→ gitsanity
commit 82f5e470d317af6503d15b586d36058564c00c5d
Author: Irakli Nadareishvili <email redacted>
Date:   Sun Jan 24 16:42:12 2016 -0500

    edited for clarity

 _posts/2016-01-24-check-what-will-push-and-brnach-for-features.md | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

commit 95cf7fa0c6eb708080c46ba1799471ede5e435b2
Author: Irakli Nadareishvili <email redacted>
Date:   Sun Jan 24 16:40:01 2016 -0500

    fixed typo

 _posts/2016-01-24-check-what-will-push-and-brnach-for-features.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

which shows the list of commits that haven't been pushed to the remote server, yet. Voila!

Quickly creating feature branches

While we are on the subject, making changes to the main branch is usually an anti-pattern for code development. Most teams create "feature branches" where they do development and submit the diff as "pull request" once they are done.

Libraries such as git flow and Github's UI help with the creation of feature branches, under certain circumstances, but they are also quite opinionated and overbearing. If you don't need the rest of the baggage that comes with larger frameworks and just need a quick way of branching for features, following bash function will do it in a targeted way:

# Branch code for feature development. $1 is the name of the new branch
function branchcode() {
   if [ "$#" -ne 1 ]
   then
     # No parameter provided.
     echo "Please provide single parameter that is the name of the feature branch"  
     return 1
   fi

    git checkout -b $1
    git push --set-upstream origin $1
}
comments powered by Disqus