Why git?
Git is the answer to all the above needs. Git is a distributed version control system:
There are many options for hosting your central repository; we are recommending Bitbucket since it is free and private.
git push -u origin master
you
will need to enter the password for your Bitbucket account at the
prompt. (You can set up an ssh key if you want to avoid doing this
every time - see the
BitBucket tutorial for help on this.) Note you need to add a file before the commit/push will work.
add
files to the staging area, commit
your changes, push
them to your teammates, pull
changes made by your teammates, etc. See the following section for details.
Before using git, you need to tell it who you are. You should only have to do this once on each account that you will use to store local clones. On some systems, you may get a warning if you fail to do this; on others, it may be an outright error that prevents you from commiting your changes. To fix this, use the following two commands, substituting your own name and e-mail address for the placeholders:
git config --global user.name "Your Name"
git config --global user.email you@example.com
After this, you can use the following git commands to manipulate your repository. Note you need to be within the directory tree of your project repository for these commands to work. (See below for creating a repository by initializing an existing project directory.)
git add newfile.c
-a
option to git commit
and it will automatically add
all the files that changed since your last revision. It won't add new files, only changed files.
git commit -m "Just started writing newfile.c"
-a
option as well: git commit -a -m "changed output routine"
-- it will automatically commit all files changed.
error: failed to push some refs to 'https://me@bitbucket.org/myteam44/myrepo44.git'
: this means you need to first git pull
changes by your team-mates before committing. It only happens if a team-mate changed a file you also changed.
git push
.
commit
alone will not let your teammates see your changes, you need to commit
then push
.
git push -u origin master
- this pushes your (local) master (main) branch to origin
, the remote server. Subsequent pushes will by default go to the same branch and remote so you can leave origin master
off.
CONFLICT
error message; see Resolving Conflicts below on how to deal with that.
git pull
. You could get a CONFLICT
here, see Resolving Conflicts below on how to deal with that.
git remote add origin https://blah...blah
(the initial BitBucket setup instructions include this)
git checkout thatfileyoujustscrewedup
git status
- shows whether git is tracking them and if there is a change not yet commmitted to that file.
git diff
git log
git remote
(replies origin
usually if hooked to one remote)
git branch -a
. It probably will reply only * master remotes/origin/masterwhich means you are on the master branch and that is in fact the only branch, but if there are other branches you will see them.
git init
from inside the (likely new) directory where you want your repository to be. It creates a hidden .git folder, use ls -a
to show it.
git init
. Go to the directory in which you want the repository to appear (as a subdirectory) and type
git clone https://bitbucket.org/username/myrepo.git
(filling in the appropriate information on your repo)
git clone /home/fr34/code/thisrepo/
What if two team members accidentally overlap on a change to a file? Note that changes to different lines of the same file will automatically be merged, so the only serious conflicts happen when one person commits a change to a line where a previous commit had changed.
myfile.txt
, the other wants to say
"Howdy, world!"
commit
first and pushes. Now Goodbye changes the same
line. Goodbye's commit
will go through, but upon push
Goodbye will get something like
CONFLICT (content): Merge conflict in myfile.txt
myfile.txt
now, Goodbye will see something like
<<<<<<< HEAD Howdy, world! ======= Goodbye World! >>>>>>> c0ebf4a3d93ee7288c6c6f216e69e7b019a1f27fThe
<<<<<<
through ========
lines are Howdy's, and the =======
to >>>>>>>
are Goodbye's lines. These lines are around each place there is a conflict, make sure to look through the whole file.
commit -a
and push
it to get the remote repository back in sync.
You should make repository-using as easy as possible for the other people you're working with by only pushing code that compiles. It is also good practice to agree to a common coding style and only push code that has been style checked.
The above covers only the most basic git features. You can also branch
off a new version, just fetch
changes without also merging explictly, rebase
your changes over the remote changes, merge
branches, stash
a particular state, tag
a commit with a label to find it later, etc. Ouch, I'm getting a headache from all these options!