0

Guys I'm a first year computer science college student and new to programming and git. I am working on a school project teamed up with 3 other people.

I tried to go through some git tutorials but they are quite long and have too many details. I'd like to ask you guys about the minimal set of git command so that I can start working on the project.

Here is what I know so far about how to start working on the project

  1. to start the project I first need to git clone
  2. Assuming now I'm on the master branch, I need to create a new branch base off of the master brach by git checkout -b <newBranch>
  3. After some coding, I git add . and if it is ready to commit, I git commit then finally git push

So here is my first question, assuming I continue to work on the same feature, i.e. I stay in the same branch, the next commit should be git commit —amend or still git commit? I understand that git commit —amend lets you combine staged changes with the previous commit instead of creating an entirely new commit. So I am not sure if the two commits are about the implementation of the same feature, should I combine them or not?

And if someone in my team made some changes in the repo, I need to sync my local master with remote master, I did some research and I know there are probably two ways to go about doing it.

One approach is

  1. git checkout master to switch to the master branch
  2. git pull to get the latest code
  3. git branch -b <newBranch> to create a branch tracking the master to work with

Then the other approach is

  1. git fetch
  2. git checkout master
  3. git reset —hard origin/master
  4. git branch -b <newBranch>

So here is my second question, are these two approach correct and are they the same? So can you guys suggest some other approach for syncing my local master with remote master

Joji
  • 4,703
  • 7
  • 41
  • 86
  • 3
    Unfortunately, this question is actually quite broad. You might get some quick and small answers here but in my opinion that is going to do you a disservice. Git is a quite complex tool and if you don't know what you're doing with it, most likely you'll be back here later with something like "Hey guys, I followed this quick tutorial and it told me to do a git reset --hard, and now all my changes are gone, how do I get them back?". Don't be that guy. At the very least, stay away from reset, rebase, amend, force push. – Lasse V. Karlsen Jul 21 '19 at 18:53
  • 1
    Your last question here is actually impossible to answer. "Are those two approaches correct". You didn't state what you wanted to accomplish **exactly**. Both of them will "sync up" your local repository with the remote, but they do so in different ways. For instance, to sync up the master branch, all you need is point 1 and 2 on that first list, checkout and pull. If you also want to start on a new feature, include point 3. – Lasse V. Karlsen Jul 21 '19 at 18:54
  • The last approach, I would stay away from that. If you have made commits on master, and then use that reset command, those commits are no longer part of the history and you will probably be back here with questions how to get them back. Not impossible, but if you're learning git you should stay as far away from reset, rebase, amend and force pushing as you can, this will hopefully avoid removing commits from your history. – Lasse V. Karlsen Jul 21 '19 at 18:55

2 Answers2

1

Ad 1: Both approaches are possible, it depends on the practices the team you work with prefers. Some teams prefer single commit per feature, some teams favour smaller commits each introducing a separated logical step towards the feature's implementation.

Ad 2: I don't understand how the last step in both approaches is related to updating the master branch. git pull is git fetch + git merge, but reset --hard is also possible - but be sure to check there are no changes in your working copy, they would be completely lost. After the master is synced, it's possible to git rebase all feature branches that haven't been pushed yet to include the new master into them.

choroba
  • 231,213
  • 25
  • 204
  • 289
1

I'll just mention a few points that you might find useful. What I tend to do while working on a feature is that I create multiple commits and then later I squash them using interactive rebase. Interactive rebase is a pretty useful command which can do a lot of things like reordering commits, changing commit messages and much more.

Another command that you might useful is git reflog. You can use reflog to go back to particular state of your project in case you mess up.

In git there are multiple ways to do the same thing. I use the following set of commands for syncing my repo -

git fetch origin master

git rebase origin/master

When using rebase you have to make sure that your working directory is clean. And at times you might get merge-conflicts while using rebase which you will have to resolve. There are other ways to sync a local repository but I usually use the method mentioned above.

Abhilash G
  • 61
  • 5
  • thanks for your answer. Should `git fetch origin master` be `git fetch origin/master`? – Joji Jul 21 '19 at 20:46