Git
Git is a control version software (CVS).
Commands
Basic commands
git init: Initialize a local repository in current directory.git add .: Add all modified files from current directory to the stage area.git commit -m: Commit all changes in current stage area to the repo.git status: Show the working tree statusgit log: Show commit logsgit checkout commit-id: Moves to the indicated commit
Remote repositories
git clone [repo-url]: Clone remote repository in local filesystem.git push: Upload changes to remote repositorygit pull: Download and integrate remote changes into local repository
Configuration
git config --list: Show current configurationgit config var-name=var-value: Set a value for the indicated config variable
Git first steps tutorial
- Create a new directory to host a repository:
mkdir new-repo - Enter the directory and Initialize new repo:
cd new-repoandgit init - Update configuration (if needed):
git config user.name myname+git confit user.email my-email@domain.com - Verify my current configuration:
git config --list - View repo status:
git status - Create a new file:
echo Hi > greetings.txt - View new status:
git status - Add new file to staging area:
git add greetings.txt - Commit changes to repository:
git commit -m 'changes added' - View status:
git status - View history log:
git log - Create a new file:
echo Bye > goodbye.txt - View new status:
git status - Add new file to staging area:
git add goodbye.txt - Commit changes to repository:
git commit -m 'new file added' - View status:
git status - View history log:
git log --oneline - List files:
ls - Move to previous state:
git checkout commit-id - Verify current files in working directory:
ls - View history log:
git log --oneline --all - Back to the 'future':
git checkout branch-name (master or main)
Git branches and merge
You can use branches to manage many lines of develpment independently. To incorporate the content of a new branch in the main branch you have the merge function.
git log --oneline --graph; this combination can be useful to understand branches.git branch newbranch: create a new branch called newbranchgit branch: view branchesgit switch newbranch: switches to newbranchgit merge newbranch: merge newbranch in the current branch
Merge Fast-Forward
When you create a new branch and you make many commits you can merge this branch into main. If main doesn't have any commits since the new branch creation the merge function will use the Fast-Forward technique.
- (main) >
git branch newbranch - (main) >
git switch newbranch - (nova_branch) >
touch file1.txt - (nova_branch) >
git add . - (nova_branch) >
git commit -m "file.txt added" - (nova_branch) >
switch main - (main) >
git merge nova_branch
Merge Three Way (without conflicts)
Merge Three Way without conflicts happen when you merge a new branch with commits and the main branch with commits too. If the commits of the two branches don't involve the same files then there won't be any conflitcs. The merge is automatic.
- (main) >
git branch newbranch - (main) >
git switch newbranch - (nova_branch) >
touch file1.txt - (nova_branch) >
git add . - (nova_branch) >
git commit -m "file.txt added" - (nova_branch) >
git switch main - (main) >
touch file2.txt - (main) >
git add . - (main) >
git commit -m - (main) >
git merge nova_branch
Merge Three Way (with conflicts)
Merge Three Way without conflicts happen when you merge a new branch with commits and the main branch with commits too. If the commits of the two branches involve the same files then there will be conflitcs. The merge is not automatic and the user have to decide wich code to save and commit.
- (main) >
git branch newbranch - (main) >
git switch newbranch - (nova_branch) >
echo "pan" > lista_compra.txt - (nova_branch) >
git add . - (nova_branch) >
git commit -m "pan added" - (nova_branch) >
switch main - (main) >
echo "queixo" > lista_compra.txt - (main) >
git add . - (main) >
git commit -m - (main) >
git merge nova_branch - Git stops the merge operation and ask the user to resolve the conflicts
nano lista_compra.txt- (main) >
git add lista_compra.txt - (main) >
git commit -m "lista da compra completa"
GitHub
GitHub is a platform and cloud-based services for software development and version control using Git. It provides repositories for software developers.
Github first steps
How to clone a repository from Github, add changes locally and send them back to the remote repository.
- Clone the remote repository:
git clone git@github.com:some_github_user/repo-name.git - Enter the local repo directory:
cd repo-name - Add new files or update existing ones
- Add changes to Stage Area:
git add . - Confirm changes in Stage Area to Repository:
git commit -m "comment" - Send changes to remote repository:
git push
The git push command requires authentication. You need to create an authentication token in the Github web to use as a password in each push operation.
SSH Authentication
You can user your RSA SSH public key to authenticate in Github:
- Confirm that you're using the right remote:
git remote -v. Are your remotes pointing to git@github:username/reponame.git ? - Go to your Profile Settings and paste your public RSA public Key in the 'SSH and GPG keys' (New SSH key button).
- Git will use your default RSA Key in your next
pushoperation.
Github flows
When you code with more people you need to stablish certain rules to coordinate work, this is called a workflow. The are many different workflows, for example:
- Feature Branch Workflow: Each new feature or fix should be developed in its own branch. When the feature is complete it gets merged into master (or main). This workflow is good for small teams and projects with few contributors.
- Gitflow Workflow: It's a more complex workflow that includes multiple branches to support features, releases and hotfixes.
We suggest the following simplified version of the Feature Branch Workflow
A simple feature branch workflow for two
Create repository on GitHub
Clone repository locally
Time to work!
Create your own branch (git branch new_branch)
Switch to your branch (git switch new_branch)
Work + Commit + Work + Commit...
Return to main (git switch main)
Check if there’s anything new on GitHub and bring it to main (git pull)
Merge your branch into main (git merge new_branch)
Push updated changes to GitHub (git push)
Back to “Time to work!”
Note: Each time, we use a different branch name: new_branch1, new_branch2, ...
If you want, you can push your local branch to the remote repository if it isn't ready to merge with main. The first time you do this you'll have to make a git push --set-upstream origin new_branch. Then just use git push for subsequent pushes.