Resource Pages
Overview
Git is a powerful tool for tracking changes in your code and collaborating with others. GitLab is the website where your remote repositories are hosted. Open a terminal, create a folder named gitlab, and work through the steps below.
Configuration and Basic Repo Workflow
-
Configuring Git
Replace
John Galtandjgalt@example.comwith your name and email.$ git config --global user.name "John Galt" $ git config --global user.email jgalt@example.com $ git config --global push.default simple $ git config --global color.ui true $ git config --global init.defaultBranch mainIf you are using macOS or Linux:
$ git config --global core.autocrlf inputIf you are using Windows:
$ git config --global core.autocrlf trueNote: The
$marks the terminal prompt. It is not part of the command. -
Checking the Git configuration
$ git config -l user.name=John Galt user.email=jgalt@example.com push.default=simple color.ui=true init.defaultBranch=main core.autocrlf=input ... -
Creating a private code repo on GitLab
- Log in to your GitLab account.
- Click New project or Create blank project.
- Choose a project name, such as
my-repo. - Select the visibility level as private.
- Select initialize repository with a README.
- Click Create project.
-
Cloning a code repo to your local machine
$ git clone https://gitlab.com/jgalt/my-repo.gitThis clones the private code repo,
my-repo, to your local machine. -
Alternative way to create and connect a repo
Before pushing, create an empty project named
another-repoon GitLab without initializing it with a README.The example below uses
touch README.md, which works in Git Bash, macOS, Linux, or WSL. In Windows PowerShell, useNew-Item README.md -ItemType File. In Windows Command Prompt, usetype nul > README.md.$ cd .. $ mkdir another-repo $ cd another-repo $ touch README.md $ git init -b main $ git remote add origin https://gitlab.com/jgalt/another-repo.git $ git add README.md $ git commit -m 'first commit' $ git push -u origin mainThis creates the local repo
another-repo, connects it to the empty GitLab project, and pushes the first commit. -
Syncing with a repo
$ cd my-repo $ git pull Already up-to-date. -
Checking the status of a repo
$ cd my-repo $ git status # On branch main nothing to commit, working tree clean -
Adding to a repo
$ cd .. $ cd my-repo $ git add Foo.py $ git add Bar.py $ git commit -m "another commit" $ git push origin main # or simply: $ git push -
Undo adding to a repo before committing
$ git restore --staged <filename> -
Undo a commit
$ git reset --soft HEAD~1This undoes the most recent local commit but keeps the changes staged. Use this only for commits that have not been pushed. If you already pushed the commit, ask before rewriting history, or use
git revertinstead.
Forking and Branching
-
Forking a repo
- Get the HTTPS link of the repo you need to fork, such as
https://gitlab.com/fareszf/cs373-collatz.git. - Click the fork button.
- When prompted, select a namespace to fork the project.
Note: After forking, the name and HTTPS link of the repo will be the same as the cloned repo, such as
https://gitlab.com/YourGitLabID/cs373-collatz.git.To rename the repo from
cs373-collatztocollatzand change the HTTPS link tohttps://gitlab.com/YourGitLabID/collatz.git:- Select the repo
cs373-collatz. - Go to the bottom of the left menu and click Settings.
- Click Advanced and go to rename repository.
- Get the HTTPS link of the repo you need to fork, such as
-
Creating a local dev branch
$ cd .. $ cd another-repo $ git branch dev $ git checkout dev # or combine both commands: $ git checkout -b dev Switched to a new branch 'dev' $ git status On branch dev No commits yet nothing to commit (create/copy files and use "git add" to track) $ touch newfile -
Listing all local branches and highlighting the current branch
$ git branch * dev main -
Pushing to a remote, not-yet-created dev branch
$ git add . $ git commit -m "Pushing to remote non-existing branch" $ git push origin dev -
Merging dev branch with main branch
$ git checkout main $ git merge devIf there are merge conflicts, resolve them, then run
git addandgit commit. -
Pushing after merging dev with main
$ git push
Undo with Git
For more examples, see How to undo almost anything with Git.