Escolar Documentos
Profissional Documentos
Cultura Documentos
co m
You're already using source control f or managing your code, right? You might even be using your SCM as the central piece of your workf low, like we do at New Relic. In this article, we're not going to review the basics of source control management, regardless of which one you use. Let's just assume that you already know how to get around. What we are going to cover is how the pros use git. We'll take a look at some of the advanced f eatures and workf lows that you might not already be f amiliar with. Hopef ully, youll walk away with your mouth agape at the sheer possibilities that git provides!
If you are anything like me, you love to explore how other developers work.
For the uninitiated, or those coming f rom another SCM, Git is a distributed version control system. It is f ree and open source, has a tiny f ootprint, and can f it within the workf low that suits you best. Generally, it doesn't f orce you to work in a particular way, which means there are many dif f erent methodologies on how to use its f eatures, like staging areas, branching and tagging releases. If you are anything like me, you love to explore how other developers work. So get ready to start modif ying your .git cong, because you're in f or a treat. Lets see how the pros use git.
You are certainly f amiliar with adding f iles to the staging area with the appropriately named add command. And you are likely f amiliar with accidentally modif ying a single f ile f or two dif f erent reasons without committing in between. So you will sure have a git log f illed with messages like "Edit X and change unrelated Y". If this sounds like your workf low, then interactive adding is your new best f riend. Interactive adding, or adding a patch, walks you through your changes one hunk at a time. When you add a f ile with the -p command, you will be prompted at each logical change (i.e., successively edited lines will be grouped together). T here are a number of choices you can make on each hunk, f rom splitting the current hunk into smaller ones, skipping a hunk, or even manually editing it. Use the ? option to see a complete list of commands. Getting started with staging hunks is as simple as: 1 git add -p <FILE>
As a good coding citizen, when you come across something that needs a quick f ix or cleanup, you should probably take a moment to change it. But if you are using a heavy f eature-branch workf low, then you don't want that unrelated f ix in your f eature branch. T his means you'll need to st ash your current changes, change to your master branch and then make the f ix there. Bouncing around between branches can be tedious, but, luckily, there is a quick shortcut f or switching to your last branch. (via Z ach Holman) 1 git checkout -
T his syntax should look pretty f amiliar to *NIX users. T he cd command has a similar shortcut (cd -) that will jump to the last directory you were in. Youll never have to remember what you named that f eature branch when you need to switch back; just git checkout -.
T he reverse is also available. Show which branches haven't been merged into the currently selected branch with: 1 git branch --no-merged
Mash this up with a couple easy UNIX tools and you can quickly delete everything that has already been merged: 1 git branch --merged |xargs git branch -d
While you could type this command each time, I highly recommend making it an alias and save yourself some serious headaches. 1 git cong --globalalias.lat est "for-each-ref --sort =-commit t erdat e --format ='%(commit t erdat e:short ) %(refname:short ) [%(commit t ername)]'"
You probably have a f riend who has accidentally committed sensitive data to a repo: access keys, passwords, your grandmother's secret marinara recipe. T he f irst thing they should do is change their passwords and revoke access with those keys (and apologize to your grandmother). Next, you'll want to hunt down the of f ending f ile and remove it f rom the entire git history, which sounds f ar easier than it
actually is. Af ter this process is complete, anyone that pulls in the cleaned changes will have the sensitive data removed as well. Forks of your repo that do not merge your upstream changes will still contain the compromised f iles (so don't skip changing passwords and revoking access keys). First, we'll rewrite the git history f or each branch, removing the f ile with the sensitive data. 1 git lt er-branch --index-lt er'git rm --cached --ignore-unmat ch <FILENAME>' --prune-empt y --t ag-name-lt ercat -- --all
Add the f ile to .git ignore and commit to update .git ignore. 1 2 3 echo <FILENAME> >> .git ignore git add .git ignore git commit -m"Add sensit ive <FILENAME> le t o git ignore"
Since we are rewriting history, you'll need to f orce push the changes to your remote. 1 git push origin mast er --force
T he compromised f iles still exist in your local repo, so you'll need to do a f ew clean-up tasks to purge them entirely. 1 2 3 4 rm -rf .git /refs/original/ git reog expire --expire=now --all git gc --prune=now git gc --aggressive --prune=now
Your f riend's repo should be f ree of sensitive data and you'll be the hero f or helping them with your pro git knowledge. (via StackOverf low and GitHub)
inherited a codebase that you can't ensure is saf e to open source, maybe you're just going to try something entirely new, or maybe you're adding a branch that serves a separate purpose that you want maintained with the repo (like GitHub Pages). For this case, there is a very simple way to create a new branch in your repo that essentially has no history. (via Nicola Paolucci) 1 git checkout --orphan <NEWBRANCH>
No discussion of git would be complete without talking about various aliases that will literally save you minutes a year in saved keystrokes. Stop wasting time typing long commands and make yourself a f ew usef ul aliases. Aliases can be made by adding them to your .gitconf ig f ile or using the command-line git cong --global alias.<NAME> "<COMMAND>". Below are just a sample of alias that you can use as a springboard f or ideas. co: with a f eature branch workf low, you'll be moving between branches regularly. Save yourself six characters every time. 1 co = checkout
ds: it is always best practice to review the changes you're going to commit bef ore making the actual commit. T his allows you to catch typos, accidental inclusion of sensitive data and grouping code into logical groups. Stage your changes and then use git ds to see the dif f of those changes. 1 ds =di --st aged
st: you should be pretty f amiliar with the verbose output of git status. At some point you'll want to skip all the f ormality and get down to business. T his alias shows the short f orm of status and includes the branch details. 1 st = st at us -sb
amend: did you f orget to include a f ile with your last commit, or maybe you had one tweak you needed to make? Amend the staged changes to your last commit. 1 amend = commit --amend -C HEAD
undo: sometimes, amending your last commit isn't enough and you'll need to undo it instead. T his alias will step back one commit and leave the changes f rom that commit staged. Now you can make additional changes, or recommit with a new message. 1 undo = reset --soft HEAD^
ls: working on a codebase with a group of developers means trying to keep up with what people are working on. T his alias will provide a one line git log including date and committer name. 1 ls = log --pret t y=format : "%C(yellow)%h %C(blue)%ad%C(red)%d %C(reset )%s%C(green) [%cn]" --decorat e --dat e =short
standup: this alias is great f or reviewing what you worked on yesterday f or any type of daily standup, or just to ref resh your memory in the morning. 1 st andup = log --since'1 day ago' --oneline --aut hor <YOUREMAIL>
graph: a complex git history can be dif f icult to review in a straight line. Using the graph f lag shows you how and when commits were added to the current branch. 1 graph = log --graph --pret t y=format ':%C(yellow)%h%Cblue%d%Creset %s %C(whit e) %an, %ar%Creset '
In Closing
Git can be both amazingly simple and mind-blowingly complex. You can start with the basics and work yourself into more complex graph manipulation over time. T here's no need to grok all of it bef ore you can use it. T he command that will be most powerf ul as you learn is man git -<command>-<name>. Try using it bef ore you ref er to Google f or an answer.