GIT

Creating your workspace

he first thing we need to do is create our workspace environment:

user@host ~ $ mkdir -p ~/git/testing ; cd ~/git/testing

Once all your project files are in your workspace, you need to start tracking your files with git. The next step explains that process.

Converting an existing project into a workspace environment

user@host ~/git/testing $ git init
Initialized empty Git repository in /home/user/git/testing/.git/

Once your have initialized your new empty repository, you can add your files.

The following will add all files and directories to your newly created repository:

user@host ~/git/testing $ git add .

Creating a commit message

user@host ~/git/testing $ git commit -m "Initial Commit" -a
[master (root-commit) 1b830f8] initial commit
 0 files changed
 create mode 100644 file

user@host ~/git/testing $ git commit -m "Initial Commit" file

To specify a particular file to commit. To add additional files or directories, you just add a space separated list to the end of that command.

Pushing changes to a remote server

That’s certainly an option to use git locally, if you want to have any easy way to have version control of your files. If you want to work with a team of developers, however, you’re going to need to push changes to a remote server.

user@host ~/git/testing $ git remote add origin ssh://git@git.domain.tld/repository.git 
user@host ~/git/testing $ git remote -v
origin	ssh://git@git.domain.tld/repository.git (fetch)
origin	ssh://git@git.domain.tld/repository.git (push)

Once you have a remote configured, you are now able to push your code.

You can push code to a remote server by typing the following:

user@host ~/git/testing $ git push origin master
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 266 bytes, done.
Total 3 (delta 1), reused 1 (delta 0)
To ssh://git@git.domain.tld/repository.git
   0e78fdf..e6a8ddc  master -> master

“git push” tells git that we want to push our changes, “origin” is the name of our newly-configured remote server and “master” is the name of the first branch.

In the future, when you have commits that you want to push to the server, you can simply type “git push”.

 

Viewing branches

git branch -a

* master    
  remotes/origin/master

Creating branches

We are going to treat the default “master” branch as our production and therefore need to create a single branch for development, or pre-production.

git checkout -b develop

You can switch back and forth between your two branches, by using the git checkout command:
git checkout master

or

git checkout develop

Making changes to our develop branch

touch develop
git add develop
git commit -m "develop file" develop
git branch
* develop
  master
ls
develop file

Merging code between branches

git checkout master
git branch
develop 
* master
ls
file

The process of moving code between branches (often from development to production) is known as merging.
we want to merge from our develop branch, where the "develop" file exists, to our master branch.
we want to be on the branch that we want to merge to.

One of the options that we can pass to the merge command, namely “–no-ff”, means we want git to retain all of the commit messages prior to the merge. This will make tracking changes easier in the future.

To merge the changes from the develop branch to the master branch, type the following:

git merge develop --no-ff

The last thing we now need to do, to make this change on our remote server is to push our changes, which we can do with the help of the git push command.

git push

Scenario 10: I’ve got a commit that I want to place on top of my current head.

git cherry-pick sha_of_commit

Scenario 11: I forgot what I just did. What did I do again?

git reflog

Posted in ITMS

Leave a Reply

Your email address will not be published. Required fields are marked *

*