January 16, 2023

Git commands

Initialise an empty git repo

1
2
3
4
5
6
# Go to your project folder where you want to initialise a git repo
cd TestRepo

# Initialise a .git directory inside the folder TestRepo
# .git directory is invisible by default
git init

Clone remote git repository to local

1
2
3
4
5
6
7
8
9
10
# Syntax
http[s]://host.xz[:port]/path/to/repo.git/
# Eg
git clone https://github.com/JinchuanL/TestRepo.git
git clone https://12.23.34.45:1234/repo.git

# Syntax
ssh://[user@]host.xz[:port]/path/to/repo.git/
git://host.xz[:port]/path/to/repo.git/
ftp[s]://host.xz[:port]/path/to/repo.git/

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
2
3
4
5
# First time push
git push -u origin main

# Future push can be
git 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 future git 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
2
3
4
# Delete .DS_Store file
git rm .DS_Store
git commit -m "Delete DS_Store"
git push

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
2
3
4
# Syntax
git push -f <remote> <branch>
# Eg
git push -f origin main

Develop a new feature on a new branch

  1. 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 dev
  2. Start development on the dev branch

  3. Commit changes to the dev branch

    1
    2
    git add .
    git commit -m "Add feature X"
  4. 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.

    1. Fetch the latest changes from the remote repository to their local repository.
      git fetch origin
    2. Switch to the dev branch to see your contributions.
      git checkout dev
    3. Merge your changes in the remote repo into their local dev branch
      git merge origin/dev
    4. Resolve conflicts if exist
    5. Test and review
    6. 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
  5. Testing new feature on branch dev

  6. Merge the dev branch into the main branch

    1
    2
    3
    4
    # Swap to the main branch
    git checkout main
    # Merge changes from the dev branch to the current branch
    git merge dev
  7. Resolve conflicts if exist

  8. Push the main branch to the remote repo

    1
    git push origin main



Reference

About this Post

This post is written by Andy, licensed under CC BY-NC 4.0.

#Git