git

git checkout

If a remote repository has a new branch new_branch and you want to take a look at it, simply do:
git checkout new_branch

Bash script: how does the $ wildcard work in git script?

I checked that the following is true, but I still don't understand why or how it works:

#!/bin/bash

git checkout production.release.4.2
production_branch=`git branch | grep "production"`

# May not give the expected result:
# the character $ is being treated as a wildcard and will be expanded, so that the output may include all files in the directory:
echo $production_branch
# Using double quotes solve the problem:
echo "$production_branch"

git: git branch name programmatically

In a bash script, you can get the current branch's name programmatically thus:

#!/bin/bash

branch_name=$(git symbolic-ref -q HEAD)
echo $branch_name
branch_name=${branch_name##refs/heads/}
echo $branch_name
branch_name=${branch_name:-HEAD}
echo $branch_name

Beware, to find a branch name (whether checked out or not) according to a pattern:


#!/bin/bash

git checkout production.release.4.2
production_branch=`git branch | grep "production"`

# May not give the expected result:

Create git commands and aliases

We can easily create new commands so that we can do the same routine by issuing a single command.

If you want to create the command git my_command, simply create a script called git-my_command in your language of choice (bash, python, etc.), make it executable and put it somewhere in your path.

git: diff between two branches

Diff between two branches:

git diff branch_1..branch_2

Syndicate content