Before I started using Git as my SCM, I would "throughly" test the code for stability then I'd just copy the working directory and rename it to something like (date)project_name. Then if I messed up somewhere and couldn't dig myself out I would just start from the last stable directory. Then I heard about Git.
I want to know if I'm using Git correctly so far. This is what I've been doing:
write some code...
git add .
to add all the changed files to the stage
git status
to check if those changed files are ready to be committed
git commit
to commit the latest changes and write a commit message
repeat
So far this is all I've been doing. For simple backup purposes and for the ability to revert back to a "previous stability" is this enough knowledge of Git? If not, what else should I know?
In all, getting familiar with Git and its features is not demanding at all. You can easily learn how to use Git for monitoring all the changes made to the source code of your projects. Also, once you start working with Git, you'll notice how you can make the most out of it with just a few simple commands.
git pull is one of the 4 remote operations within Git. Without running git pull , your local repository will never be updated with changes from the remote. git pull should be used every day you interact with a repository with a remote, at the minimum. That's why git pull is one of the most used Git commands.
As others have mentioned, I highly recommend getting comfortable with branches. My workflow is typically:
Starting form the master* branch:
git checkout -b awesome-new-killer-feature
create a new branch (-b) and have it checked out.
write some code ...
git add .
, git status
, git commit
commit small changes, repeat step 2
OH no! My friend just reported a serious bug! He lost data!!!!!
git checkout master
go back to the master branch
git checkout -b bugfix-serious-data-loss
create new branch for hotfix
fix bugs, git add
, git status
, git commit
, rinse, repeat until bug is fixed
git checkout master
go back to master branch
git merge --no-ff bugfix-serious-data-loss
merge bug fix back onto master
OK, now I can get back to working on my awesome-new-killer-feature:
git checkout awesome-new-killer-feature
resume working on what I was working on
git rebase master
merge back changes to master onto working code so we get the benefit of the bugfix. Not to mention this reduces the probability of merge conflicts later on when we need to merge this branch back to master
write code, git add
, git status
, git commit
, rinse, repeat until feature is complete
git checkout master
, git merge --no-ff awesome-new-killer-feature
merge the branch back onto master
Now sit back and type gitk
to see a nice historical view of what you've been doing.
Optional:
git branch -D bugfix-serious-data-loss awesome-new-killer-feature
delete unused branches. I like to keep my repo cleanThe power of git comes not from being able to checkpoint your work. It comes from how fast and cheap branching & merging is. Branching allows you to work on multiple ideas at the same time and/or experiment with throw-away ideas all without affecting your stable code. If the idea doesn't work simply delete the branch, if it works merge it back onto master.
*note: By convention most git users call their main/trunk branch "master".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With