Git is used for codebase management in software development.
In simple terms, there are two starting points for any project + git - you are creating a new codebase or you want to use codebase from an existing repository.
Following are some basic operations you will tend to do with git.
-
Clone a remote repo:
git clone <url>
This is your starting point if you are using codebase from an existing repository. This gives you a copy of the (terminology alert!) remote repository.
-
Initialize a local repo:
git init
This is your starting point if you are creating a new codebase. This creates a (terminology alert!) local repository.
This creates a hidden
.git
folder in the location from where this command is run (the location is typically the highest level of your project directory) along with amain
(terminology alert!) branch which is, well, the main branch of the codebase. Think: master copy of the codebase. -
Add files to the local repo:
git add <list of files>
This adds the specified files to the local repository.
-
Commit added files to the local repo:
git commit -m "<your commit message here>"
This "saves" the added files kinda like a milestone. Typically, a commit message describes the changes you made to the files.
-
Link a remote repository to a local repository:
git remote add origin <url>
If your starting point was a new codebase (not an exisiting one), then you'd want to link a remote repository with your local one so that you can backup your code, share it with others, etc.
-
Push changes to the remote repo:
git push origin <branch name>
This pushes the codebase from the specified branch of the local repository to the remote repository.
By default your codebase sits on the main
branch. but there may be different version of your existing files or additional files created by you or other developers. You can store them on different branches.
-
List available branches:
git branch
You'll see a * symbol on the active branch
-
Create a new branch:
git branch <branch name>
-
Jump onto a branch:
git checkout <branch name>
Note: You can create a branch and jump onto it in a single command -
git checkout -b <branch name>
-
Delete a branch:
git branch -d <branch name>
-
Rename a branch:
git branch -m <branch name>
You may want to update your local repository with changes from remote.
Prelininaries: Ensure you are on the right branch. If not, use git checkout
to switch to the right branch.
Fetch latest remote commits: git pull
(For more merging actions, see: https://www.atlassian.com/git/tutorials/using-branches/git-merge)
A fork creates a new copy of the remote repository that's essentially disconnected from the original remote repository. Thus a fork is associated with two remote repositories - 1. the original remote repo (which you cloned or linked with using git remote add origin <url of the original repo>
) and 2. a new remote repo (which you'll create and link with using git remote add upstream <url of the fork>
).
Now you can push to whichever remote repo you want from the local using git push origin <branch name>
(to push to original repo) or git push upstream <branch name>
.
If you have to sync changes from your original remote repo to your fork, a quick way to do that is by clicking "Sync fork" on Github.com page of your fork to update the remote fork.
If you have local clone of the fork, update it with git pull
.