As developers, we use source control all the time, at least any good software developer should and if you're not what decade are you in? In previous positions, I've used Team Foundation Server's Version. This is a centralized approach, similar to Subversion or a host of others. The concept is that a developer checks out a file to work on and locking it, so others may not alter the same file. Git has more of a learning curve than centralized source control types such as SvN or TFSVC. Git uses a distributed approach. This approach to source control takes some additional thought and practice to get used to, but it also offers greater flexibility.
When learning to use Git, I had to get over the fear of 'messing things up'. It is source control after all and it does retain history (As long as you don't rebase your branch). What I've found is that when working on a feature or task, it is important to communicate with others, and push changes and pull frequently to and from your remote branches (more on this), as well as commit often.
On my team, we usually use the master branch for the stable code base which has been tested and ready for production. There is a development branch that is used for pull's and push's to and from local versions of your repositories, and only merge the master branch with the development branch after testing on the new feature or bug fix has occurred.
Below are some of the common commands that I find necessary to understand in order to work successfully with Git. Included is a cheat sheet from GitHub that has proven useful. If you are as ambitious to learn about Git as I am, here is a link to a free book put out by Git that covers essentially everything you may want to know (git book).
Below are a series of commands via the command line that I hope you'll find useful. This is the command to clone from remote. This does not mean that the cloned project will be created as a git repository.
$ git clone https://yourgitserver.com/user/repo.git
Use the following command to initialize new repository and place files under source control.
$ git init
Use the following commands to set your remote repository (server repository), as well as to view remote branches.
$ git remote add origin https://yourgitserver.com/user/repo.git $ git remote -v #output from running the above command. This will show you where your remote branches are. origin https://yourgitserver.com/user/repo.git (fetch) origin https://yourgitserver.com/user/repo.git (push) #the above are now the origin for my remote. I can create branches on my remote, but if I push it will be to the default name of master #often times we will work with various branches on the remote as well. #in that case use the following command $ git push origin current_branch_name:
Here is more information about pushing and creating a remote:
Git Push Documentation In this example we're cloning a specific remote repository. You may be asked for credentials, at my current employer we actually use SSH for communication with the repository (Not HTTPS) which requires the setup of SSH Keys and by default does not require password authentication. In this example, we are also assuming that there is only one branch on our remote that we are concerned with (origin). What can sometimes be a bit confusing for people who are new to the decentralized way in which git operates is just that, it's decentralized. I found it is crucial to communicate to others when I have done a push of anything substantial that others may also be working in as well. This can create merge conflicts and sometimes ones that could have been avoided. This is where the power of branching lies. In my opinion merge conflicts are unavoidable in the long run. Here is a list of some other common git commands. I prefer the command line when learning the ins and outs of Git, as it gives me a feeling that what I am doing is explicit.
$ git init #initialize a new repository $ git checkout -b newBranch #create and checkout new branch $ git branch newBranch #creates new branch in working directory $ git branch #tells you what branch you are on with * and usually a green color $ git checkout branchName #checkout a different branch $ git status #inspects the contents of the working directory and staging area, what you have committed and changes you've made $ git add filename.ext #adds specified unstaged file to staged. $ git add . #adds all unstaged files for commit. $ git diff HEAD .\ #shows the differences for the file you are working on from the HEAD (last commit) $ git commit -m "Message for the commit" #stores file changes from the staging area in the local repository $ git commit -a -m "Message for the commit" #adds and commits files from the staging are in the repository $ git log #shows a list of all previous commits, this is also filterable with other flags $ git reset HEAD #unstages all files to your last commit $ git reset fileName #unstage file to the commit in the hash $ git show HEAD #will show all the changes made to the working directory and that have been committed. $ git fetch master #will fetch the history from your master branch $ git merge master #What if you wanted include all the changes made to the branch on the master branch? #We can accomplish this by merging the branch into master with this command after you have done a fetch of the master branch. #Switch to the branch you want to merge too, then run the above command. $ git branch -d # will delete the specified branch from local repo
Here is the link to a cheat sheet that I have found helpful on GitHub :