In an effort to bring the team up and running on git, I’ve made this tiny guide of the most essential commands.
To instantiate a repository for your code, you have to create a repository, stage changes and commit them.
git init .
git add Readme.org Somefile.txt
git commit -m "Initialize Repository"
So, that’s our first commit!
At this point, the code only lives on my local machine, so it’s next to impossible to get involved. Let’s fix that!
git remote add origin git@github.com:hkjels/Git-101.git
Here we are saying that we would like to add a remote target with the local-reference origin to the URL git@github….
If we then do a
git push -u origin master
We send our first commit of to Github’s server. Notice that I use the
-u
flag. That will make this repository’s master-branch point to the
origin’s master-branch for all subsequent pushes, so that you don’t
have to specify anything other than git push
later on.
Branch, branch, branch! If you come from the SVN-lair, you might think twice before merging because of resources and the time you’ll need to spend, but with Git it’s almost instant, so branch away!
git checkout -b "fix/typo"
We are now in the fix/typo
branch, so I’ll just do that before moving on.
Whistle, whistle, tada, bum, ti, dum…
git commit -a -m "Correct the typo"
Note, I discourage the use of the ~-a~ flag, but I think it’s OK for demonstration purposes
Let’s jump into our master-branch
git checkout master
From here, we can now merge in the changes made to our fix/typo
branch.
git merge fix/typo
git push
The corrected code now lives in a remote repository, so others can pitch in.
Now, imagine for a second that someone else did that last commit.
git reset HEAD~ --hard
Here we reset the tip of this branch (master) to one commit earlier than HEAD, which is the last change that was pushed.
We can simply do a
git pull
And now our master-branch is aligned with the remote origin’s master-branch.
Again, lets imagine that someone else did that last commit.
git reset HEAD~ --hard
Now, I will make a change to the same line as that other doofus and we
will end up with a merge-conflict when I try to pull
.
git commit -a -m "Make the pitch friendlier"
This will fail, for the obvious reason that we are behind the remote origin’s master.
git push
So we have to retrieve the changes. And yes, this will fail as well, because we have a conflict.
git pull
Resolving that piece of shit!..
git rebase --continue
git push