Git-Logo-2Color

Useful Git workflows and Tips

These are some workflows which I find useful while working at my job. Hopefully they can save you some time while you wrap your head around around Git basics.

Git branching: The commands below checks out the new feature branch from the master. Then so some work and do commits. Then checking out master branch  and merge to the master branch.

  • git checkout feature_xyz
  • git commit -a -m “I am committing now”
  • git checkout master
  • git merge feature_xyz

Pushing a feature branch to the Git repository:

  • git checkout feature_x
  • git push origin feature_x

Amending the last commit:
git commit –amend -m “New commit message”

Generating new keys and adding them – Client side

  • ssh-keygen -t rsa -C “your_email@example.com”
  • ssh-add your_name

If you get the error “Could not open a connection to your authentication agent” in Git Bash. Run the following commands to troubleshoot the problem:

  • ssh-agent
  • eval “$(ssh-agent)”
  • ssh-add your_name

If you want to permanently add keys, so that Git bash recognizes them automatically when pushing changes to the repo. Rename private and public keys to id_rsa and id_rsa.pub

On the machine where you are pushing changes (Server):
Add your_name.pub (id_rsa.pub) public key under home/your_home_directory/.ssh/
Copy id_rsa.pub file to authorized_keys file

Reverting back to particular commit
git revert ‘commit-id’

Resetting the repository to a particular commit:
git reset –hard ‘commit id’

Removing/adding (synchronizing) branches from remote: (Very useful)
git fetch -p

Cleaning up untracked files:

git clean -f

Using git mergetool (I usually use tortoise merge http://tortoisesvn.tigris.org/TortoiseMerge.html)
git mergetool [--tool=]

Fetches any new changes from the original repository:

git merge upstream/master

Creating an empty branch:

git checkout –orphan <branchname>

Deleting the remote branch:

  • git branch -d (local branch)
  • git push origin :<branchName> (remote branch)

Before pushing the feature branch to the git repository, its a good practice to get all your changes in one commit. For this, I do the following:
git rebase –i HEAD~(number of commits) // replace (number of commits with the number of commits I made to my branch)