No Images? Click here Hi everybody! I'd like to introduce you to Shaumik Daityari, author of Jump Start Git, our guide to the popular distributed version control system. Today, Shaumik is going to share some tips to help you make the most of Git. Happy Learning! 5 Top Git TipsGit is often the first choice of version control for developers when they start out on a new project. Once you've gotten a handle on the basics, you can boost your productivity by learning some lesser-known Git tricks. Here are my top five tips for becoming a Git ninja! I'll assume that you have working knowledge of Git. If you need some help with the fundamentals, my book is the perfect place to start. 1. Faster Terminal Workflow with Autocompleted Git CommandsIf you would like speed up your workflow by enabling auto completion for your Git commands on the terminal, there is a script that you can run to use the Tab key to complete your commands. First, download the bash script from GitHub. cd ~ curl https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash Next, add the following lines to your bash profile, located at ~/.bash_profile. if [ -f ~/.git-completion.bash ]; then . ~/.git-completion.bash fi 2. Remove a Branch on a Remote (in a Flash!)If you would like to remove a branch from a remote without having to browse to the location of the remote, you can run the following command from your local system: git push remote_name :branch_name This essentially pushes an empty branch to the branch_name of remote_name, thereby removing the branch from the remote. You need to have write access to the remote to perform this action. 3. Save Uncommitted Changes with StashesAre you working on a change, but need to leave it and focus on something else in the meantime? You probably don't want to commit an incomplete feature into Git's history. The ability to stash your changes away comes to your rescue! With uncommitted changes to the repository, simply use the following command to stash your changes away for future use. git stash You can check the saved stashes using the following command: git stash list To retrieve the uncommitted changes from the stash, use the git stash apply command and specify which stash you want to retrieve. For example, use the following command to apply the stash with an identifier 1: git stash apply stash@{1} 4. Selectively Commit Changes from a FileBy design, a commit should contain a single logical change to your code base. If a single file contains multiple changes, and you like each of those changes to have their own separate commit, Git provides a patch option to the git add command. Use the -p option while running git add. git add -p Git starts an interactive process for you to work with each group of changes in a file. It allows you to selectively stage a group of changes and commit. Make sure you do not use the -a option when running the git commit command. 5. A Polite Force PushIf you have done a rebase to merge commits, or in some other way overwritten the history of a branch, pushing it to a remote would be rejected. A workaround to this reject is a --force option of the git push command, that overwrites the remote's branch's history. However, performing a forced push is considered harmful as you may inadvertently overwrite changes that were done since you last updated your local branch from the remote. The lesser-known, and lesser-evil sibling of the forced push is the --force-with-lease. It refuses to update a branch if someone else has updated it since your last pull. Start your subscription today and you'll get access to this book, plus 300+ other web design and development books in SitePoint Premium!
Until next time,
|