Initialise an empty git repo
1 | # Go to your project folder where you want to initialise a git repo |
Clone remote git repository to local
1 | # Syntax |
Git rename the current branch
1 | git branch -M main |
git branch (-m | -M) [<oldbranch>] <newbranch>
branch: List, create, or delete branches
-m | -M: <oldbranch> will be renamed to <newbranch>, if <newbranch> exists, -M must be used to force the rename to happen.
Above code will rename the current branch to “main”
-M
: Force renaming even if the new name is already exists as another branch.
Git Push
1 | # First time push |
Pushes local commits from the current branch “main” to the remote repository named “origin”
-u
: Sets the upstream branch for the current local branch. After this command, the futuregit push
command will push the “main” branch to the “origin” remote by default.
origin
: The default name for the remote repository from which the local repo is cloned or added as a remote.
Delete files from local filesystem and remote repository
1 | # Delete .DS_Store file |
Overwrite remote repository branch with local branch
This operation may cause a loss of commit in your remote repo.
Eg.
If your remote repo has 2 commit histories with a newer version of the repo.
Your local repo has only 1 commit history with an older version of the repo.
If you force push your local branch to the remote branch, the remote branch will be overwritten with your local branch commit which will make the remote branch the same as your local branch (1 commit with the older version of the repo –> loss of the second commit).
1 | # Syntax |
Develop a new feature on a new branch
Create a new branch called
dev
1
2# This will create a new branch called "dev" and swap your current branch to it
git checkout -b devStart development on the dev branch
Commit changes to the dev branch
1
2git add .
git commit -m "Add feature X"Push new features to the remote repo (if you want to share the new features with your team.)
1
git push origin dev
Your teammates can update their repo with your contributions with the following steps.
- Fetch the latest changes from the remote repository to their local repository.
git fetch origin
- Switch to the dev branch to see your contributions.
git checkout dev
- Merge your changes in the remote repo into their local dev branch
git merge origin/dev
- Resolve conflicts if exist
- Test and review
- If they want to share their updated dev branch with others or synchronize it with the remote repository, they can push their changes with
git push origin dev
- Fetch the latest changes from the remote repository to their local repository.
Testing new feature on branch
dev
Merge the
dev
branch into themain
branch1
2
3
4# Swap to the main branch
git checkout main
# Merge changes from the dev branch to the current branch
git merge devResolve conflicts if exist
Push the
main
branch to the remote repo1
git push origin main
Reference
About this Post
This post is written by Andy, licensed under CC BY-NC 4.0.