I recently started a new series called Scheme in Python mainly to teach myself a bit about branching and merging in git. This post is simply a reference for some of the git commands I regularly use, as well as some explanation about branching and merging.
I’ll start with some of the most basic git commands for creating a repo, adding files and commiting changes to those files.
Create a new repo
To create a new repo, first create a new directory and name it whatever you like. Then from the command line cd into that directory and use the following command.
$ git init
This will create a new repo in that directory. If you already had files in that directory, the git init command will not automatically add those files to the repo. To add a file use the add command.
Add files to repo
$ git add filename
Where filename is the name of the file you want to add. However this is not enough to add the file to the repo, it simply stages the file to be added in the next commit. To commit your changes to the repo you need to use the commit command.
$ git commit -a -m "Type your commit message here"
The -m option allows you to write a commit message that describes the changes you made. If this option is not present the default editor will be opened for you to write a commit message, they are mandatory. The -a tells git to commit all the changes from every file. If you only want to commit changes from a specific file, you can specify that file instead of -a.
$ git commit filename -m "Only changes in filename are committed."
Push changes to remote repo
So far all of the changes and commits have been done locally. If you want to upload your changes to a server such as github you would use the push command. However before you can do this you need to tell git where that remote repository is. Your source code hosting site should give you instructions on how to do this. Here’s an example of the command I needed to run to upload to github for Scheme in Python.
git remote add origin firstname.lastname@example.org:jacktrades/Scheme-in-Python.git
You will only need to do this once. Afterward you can just use the push command.
$ git push -u origin master
This will upload the master branch to the server. If you don’t do any branching master will be the only branch you have.
Creating and changing branches
Creating a new branch in git is simple, just use the branch command.
$ git branch v0.01
Here a new branch was created and named v0.01. You could choose any name you want for the branch, though I’m not sure exactly what characters are allowed.
Even though the v0.01 branch was created, git does not automatically switch you into it. To find out what branch you are in you can use the branch command without any other arguments.
$ git branch * master v0.01
The branch name with the asterisk(*) next to it is the branch you are currently in. To change to another branch use the checkout command.
$ git checkout v0.01
This changes the current branch to v0.01, to verify this you can use the branch command again.
$ git branch master * v0.01
When you make changes and commit them you will be commiting to the current branch (in this instance the v0.01 branch). If you make some changes, commit them then switch back to the master branch, you’ll find that the commits are not there.
For convenience git provides a method of creating a new branch and checking out a working copy of it in one command. thanks r2p2
$ git checkout -b v0.01
Pushing branches to a remote repo
To upload the new branch to the server you need to tell the push command which branch you want to push. So for the v0.01 branch you would need to do this.
$ git push origin v0.01
To push changes made in multiple branches you can leave off the branch at the end of the push command.
git push origin will push all changes on the local branches that have matching remote branches at origin.
– docgnome from stackoverflow
To merge one branch into another first checkout the branch that you want to merge into. For this example we’ll merge the v0.01 branch into master. So first we checkout the master branch then do a merge command.
$ git checkout master $ git merge v0.01
* That’s all for now, but I’ll come back and add commands for merging and how to resolve conflicts once I get the chance to use those features. This page will likely be updated a number of times as I learn more about git.