DevOps for Node.js Engineers: Setup Local Projects with?Git

DevOps for Node.js Engineers: Setup Local Projects with?Git

Initialize Git repository locally

In a previous article, we saw how to create a Git repo from cloud services like GitHub:

DevOps for Node.js Engineers: Version Control with Git

Version control with Git like a pro!itnext.io

But what if you already have a local project which is not a Git repo? And how do you push it to a hosted Git server like GitHub?

Transforming a folder into a Git?repo

Inside your local project folder, run the command

git init        

This will initialize your local Git repo by creating the?.git hidden folder which contains all the Git artifacts to track changes in your source code.

Next, if you run

git status        

you will see that all files are untracked by Git.

Next, add everything to the Git index (staging area — see the above article for more info), meaning tell Git to track changes on these files.

Optionally, you can create a?.gitignore file to indicate which files/folders Git should not track — like local config or sensitive data which should never be in Git history for obvious reasons. Do it before committing so that you don’t have to later rewrite the entire Git history because it contains secrets.

# add everything in current directory to Git index
git add .        

Next, commit the changes (save them in the Git database in?.git/)

# name your commit 
fgit commit -m "initial commit"        

Also be aware that when you initialize Git in a folder, a default branch (more on branches in another article) is created. Depending on the Git version, it will be named master or more recently main.

If you want to rename the current branch:

git branch -M my-branch        

Creating a new Git remote repository

Now that your local Git repo has been created, you need to be able to push it to the remote Git repo to share it with other developers (or just have a remote copy).

For that, you must first create a new repository on your Git cloud provider of choice.

It is a straightforward process on most platforms like Gitlab or GitHub.

Linking the remote Git repo to your local?repo

For your local Git repo to know where to push, you must add a reference to the target remote Git repo:

git remote add <NAME> <REPO_URL>
# EXAMPLE
git remote add origin [email protected]:myuser/myrepo.git        

Setting the upstream?branch

Now that your local Git knows where to push, the corresponding branch must exist on the remote repo.

The upstream branch usually has the same name as the local branch you are pushing to the remote repo, but not necessarily.

When you see “upstream branch”, we are talking about the branch on the remote Git repo (referenced byorigin) on GitHub or whatever.

To create the corresponding remote branch while pushing your commits:

git push --set-upstream <REMOTE_REFERENCE> <REMOTE_BRANCH_NAME>
# EXAMPLE
git push --set-upstream origin my-branch        

Now that this is set, Git will be able to tell the difference between the local Git history and the one on the remote repo.

Removing Git from project?folder

If for some reason you no longer want your local project to be tracked by Git, you simply delete the?.git/ folder and you’re back to a non-Git folder (and lose all the history of commits).

Don’t worry if you delete your local?.git folder by accident, it still exists on the remote Git repo. Of course, push often so that you have a recent version remotely.



See you in the next article!

要查看或添加评论,请登录

Florian GOTO的更多文章

社区洞察

其他会员也浏览了