git

Switching code to a mirrored repo

Let say you mirror your repository from gitlab to github or vs versa. You can re-clone your code from the new place.

But there is a better and faster way. First it is good idea to view your current config …

git remote -v

Then to do the change do something like this :

git remote set-url origin git@new_repo.com/something/blah.git

Now check if it changed :

git remote -v

git: Useful commands

Cloning a branch

This is mostly a notes for me… for cloning a branch directly putting it in directory of your choosing.

Here is the command :

git clone -b my-branch ssh:/.....  my-dir

you don’t have to clone, cd, then change branch …etc… and you have it in directory of your choosing.

Revert changes

This is how to revert all changes you made after the last commit. So if you think your latest changes are no good you can rollback to the last stable state, like in the databases.

git reset HEAD --hard

Start local repository

You should always put your code under version control.
Here is recipe how you start a repo with your project on your local machine. Easy peasy … so you have no excuse not to do it.

  • /repo : is the directory where you keep your repositories
  • /work : is your workspace directory
  • blah : is the name of the repo you are creating
cd /repos
mkdir blah.git
cd blah.git
git init --bare
cd ..
cd /work
git clone /repos/blah.git
cd blah
> readme.txt
git add readme.txt
git commit -m 'start ...' -a
git push

Git: creating clean branch

Sometimes you need to create a clean branch in git that has no history.
This is the case for example when you need to have two or more code bases, but you are have just a single repository. Sad but happens !
You have to do with what you have.

In this case use the following recipe :

#fetch the original repo.
#Normally you would like dir_name to be the same as the new branch name
git clone ssh://.... dir_name
cd dir_name
#create the orphan branch
git checkout --orphan blah
#cleanup all the files, you dont need them, right ? 
git rm -rf .
#add a file so you can commit
echo start > readme.txt
git commit -m 'creating an orphan branch ...' -a
#now the branch shows up
git branch
# ... no commit log, like we wanted
git log
# time to make the branch show up in the remote repo
git push -u origin blah