Git Tutorial For Beginner
Become a git guru
Adding an existing project to GitHub using the command line —
- Create a new repository on GitHub or other.
- Open Git Bash on the current project directory.
- Initialize the local directory as a Git repository
$ git init
4. Add the files in your new local repository. This stages them for the first commit.
$ git add .# Adds the files in the local repository and stages them for commit. To unstage a file, use 'git reset HEAD YOUR-FILE'.
5. Commit the files that you’ve staged in your local repository.
$ git commit -m "First commit"# Commits the tracked changes and prepares them to be pushed to a remote repository. To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again.
6. Set the new repository created.
$ git remote add origin remote-repository-URL
# Sets the new remote
$ git remote -v
# Verifies the new remote URL
7. Pull the changes in your local repository from GitHub.
$ git pull origin master
# Fetch and merge changes on the remote server to your working directory
8. Push the changes in your local repository to GitHub.
$ git push origin master
# Pushes the changes in your local repository up to the remote repository you specified as the origin
Changing a remote’s URL —
The git remote set-url
command changes an existing remote repository URL.
- Open Git Bash on the current project directory.
- Change your remote’s URL from SSH to HTTPS with the bellow command.
$ git remote set-url origin your-repository-url
3. Verify that the remote URL has changed.
$ git remote -v
# Verify new remote URL
Git Branching — Basic Branching and Merging
Git Branch
To create a new branch and switch to it at the same time, you can run the git checkout
command with the -b
switch:
$ git checkout -b branchName
creating and Switched to a new branch "branchName"
If you want to just switch one branch to another branch then run this command:
git checkout branchname
Git Merge
The “merge” command is used to integrate changes from another branch.
git checkout master
git merge branchname
#This will merge "branchname" with "master"