Jump to content
Larry Ullman's Book Forums

Recommended Posts

A quick question for Git aficionados. I used git very briefly a year ago or more. And I am starting to use it again. Or more so trying to. But I keep seem to be getting errors about the head and refs.

 

I can't remember the exact error and I'm on my phone not computer. Am I meant to be pulling everything from my repository onto to my Mac each time I want to develop or is it ok I do what I had been doing a long time ago that doesn't seem to work now which was upload my whole contents to my repositary, do some local work. Commit more changes?

Link to comment
Share on other sites

Ideally, you should keep the local and remote repositories as close to synced as possible. It'll just make merges easier. Also, I always make sure my local is up to date before creating a new branch. 

 

In theory, it doesn't matter, as Git will warn you of conflicts, but if you keep branches open for a shorter time and keep your local copy up to date with the main repo, merges will take a lot less thought. 

Personally, I update my local repo from the origin at the start of each day and before each new branch. I push my local changes to origin as soon as they're done and/or at the end of the day (after merging, of course). This is true both in organizations where there's 200 people using the same repo and for my personal projects, where I'm the only one. 

Link to comment
Share on other sites

Also, I found these two references to be pretty good:

http://gitimmersion.com/ (basics)

https://www.atlassian.com/git/tutorials/ (actual implementation)

 

And this book for understanding Git on the behind-the-scenes level: http://shop.oreilly.com/product/0636920022862.do It's good if you know the basics and usage but want to better understand the why's of using Git. 

Link to comment
Share on other sites

The biggest issue I had was understanding "the flow" of git. If you have get an error, you are doing something wrong. Some typical error cases:

 

1. You try a git pull origin master (or similar) and get an error. Your local branch is probably not clean. Do a "git status". If you have any changes, do one of two things.

- If you don't care about saving your changes, you can do something like "git checkout .". That will remove any changes to tracked files. (git checkout file/to/checkout will remove single files) If you've added files using 'git add file/to/add", you can unstage them by doing "git reset file/to/unstage" or all by git reset .". Once the files are unstaged, do a "git checkout ." again. You should now be able to pull.

 

2. You can't perform a git push origin master. (or similar)

- You are probably behing the remote. Commit you changes first, do a git pull origin master, then a git pull push origin master.

 

One of the things that took me a while to understand that comitting and pushing doesn't need to happen right after another. You can always commit changes, but you might not be able to push them to another users commits. When you pull a branch like in the second example, you might get something called a merge conflict. You'll see those as "both added" when doing a git status. Open the file and look at the git merge conflict code. You'll see code like this:

<<<<<<< HEAD
nine
ten
=======
eight
twelve
>>>>>>> branch-a

If you want to keep 'nine', remove everything but 'nine'. If you want to keep nine and twelve, remove everything but those lines. Conflicts might look a lot more complicated than this, but the same principle applies.

 

Git has become a trusted friend during the last eight months, and I'm really loving it. It is hard to understand and even harder to master, though. When you get the basics, it works like a dream. I had lots of git problems in the start, but they are all gone now.

 

Good luck, Jonathon. Git is really worth learning.

Link to comment
Share on other sites

Definitely agreed. I came to Git late and felt like I was playing catchup. It was all fine and good until it wasn't and then I was frustrated. I'm really just now getting reasonably comfortable with it (i.e., where I should have been years ago). 

 

For my own projects, it's just me developing, with the server being the origin, and I can just push everything without problem. There's no merging and it's all a breeze. It's useful and nice, and I don't have to FTP, and I can easily undo changes, but it's not the full power of Git. 

 

Working on a big team with a centralized workflow, it's always `git fetch; git rebase -i origin/master` to get other changes into a branch and then `git push` to create a PR. All fine and good until there's a conflict! It took me too long to realize that, when there's a conflict, you just use `git status` to see the conflict, open the file, edit it to reflect what it should be, then `git add; git commit`. In other words, a conflict isn't a problem, a stopping point, just something you need to look at. 

 

Also, I think it helps to remember that very few things you do are undoable (kind of the point) and you aren't really screwing up things for other people until it's on origin/master. Up until then, it's just your mess!

Link to comment
Share on other sites

 Share

×
×
  • Create New...