Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
- It allows us to save many versions of a same proyect using checkpoint
- It save us memory
- It comes to solve the problem of saving multiple files for one proyect to have checkpoints
- It works locally. Meaning that working in a group project just with git unpractical.
- If we lose or damage the computer where the project is we can lose all the change log.
- Is the largest development platform
- It's a Cloud Hosting & collaboration provider
- It is a git repository hosting
- It come to solve the git's problems
Github and Git aren't the same thing, but they complement each other
Whenever we use git, it is created a working directory and a hidden folder called .git , where the version control system works.
More information here
- Working directory: Folder where git is managed
- Commit: files and folder snapshot. It registers the changes
- Branch: Order set of commits
Check website
- git init: To initialize repository
- git status: Gives a description about the project files and the current branch. It also give us information about the current folder files, if they are in the worging directory or the staging area
- git add file_name: It add file_name to the staging area. If we use a point (.) instead of a file name, it adds all the current folder files to the staging area
- git commit -m "message": Moves files from staging area to the repository with an attached massage
- git config --global user.email "email": It allow us to configure with which mail we identify ourself inside git
- git config --global user.name "name": It allow us to configure with which name we identify ourself inside git
- git config user.email: It gives us the current user's mail
- git config user.name: It gives us the current user's name
- git log: It give us information about all the logs of the current branch
- git checkout commitId_or_branch: It allow us to restore to a previous version or to move to other branch
- git branch: It give us the names of all branches
- git branch new_branch_name: It creates another branch
- git checkout -b new_branch_name: It create a new branch and move the head to it.
- git merge other_branch: It adds all the changes of other_branch to the current branch
- git switch branch_name: To creat a move to other branch
- git switch -c branch_name: To create a new branch.
- git ls-files: To see files that are considered in the current branch for, at least, the staging area
- git add: Remove from stage area all the files deleted in the working directory
- git rm file_name: To delete file from staging area (It should have been deleted from the working directory too)
- git clean -d: To delete unstaged files from working directory
- git clean -df: To delete unstaged files from working directory
- git clean -dn: To show unstaged files from working directory and the computer
- In the working directory for one file: You can just to the latest commit state for an specific file by using the command: git checkout file_name or git restore file_name
- In the working directory for multiple files: git checkout or git restore .
Just use git clean -df
git reset file_name: It doesn't errase anything, it just move it from staging area to the work directory
- git reset --soft HEAD~1: This undo 1 commit from the branch, returning all changes done to the staging area
- git reset HEAD~1: This undo 1 commit from the branch, returning all changes done to the working directory
- git reset --hard HEAD~1: This undo 1 commit from the branch, returning 1 commit back erasing any change done.
- Just one banch: git branch -D branch_name
- Multiple branch: git branch -D branch_name1 branch_name2 You can delete a branch when there is no more need of it
- Head: Last commit we are pointing
- Detahced Head: Special branch that is created when we switch move the head to an specific commit. This special branch is erased whenever we change branch, the only way to save it is to create a new branch
Each commit always has its id. When we move the head to previous commits and we add other commit, it is generated a special id.
When we switch from this branch to other we want to know this id, so we can generate other branch (if we want). To generate this new branch we use the command: git branch new_branch_name detached _head_commit_id
- To ignore one file just write in one line its name. For instance, text.log
- To ignore files with a particular type just taling into account it type. For example: *.log. To make an exeption you can specify the files by using the next sintaxis: !exception.log
A place where we can save uncommitted and/or unstaged progress.
- git stash: To save the uncommitted and/or unstaged progress and rebuild the project to the latest commit state
- git stash apply: To rebuild the stash
- git stash push -m "message": Add changes with comments into the stash
- git stash save "message": Add changes with comments into the stash
- git stash pop stash_index: To commit a specific stash state and removed from the stash list
- git stash drop stash_index: Drop a specific stash checkpoint
- git stash drop clear: Drop all the stash checkpoints
Whenever we change from one branch to other we lose the staging area data.
When we are working in multiple branches we may need to do the same change for 2 branches.
For example, adding the same new file. We create and write the file in one of those branches. Then, we can
- Copy the file, change to the other branch and paste the file in it. And if we change this file here to we need to do the same to move the changes to the other branch.
- Use the stash, change the branch, get stash and now we have the same file for both branches. This without adding it to the stage in any of the two branches. We can work on this stasg node, modify it and be able to use the same file in both branches. Source: https://www.quora.com/Whats-the-difference-between-git-stash-and-git-stage
- git reflog: command to see all the the changes done during last 30 days, even those deleted commits. You can use the id provided to reset the branch just by using the command git reset --hard reflog_id
First clasification:
- Fast-Forward: When there are no additional commit in the master branch (after feature franch was created)
- Non Fast-Forward
Non Fast-Forward clasification:
- Recursive
- Octopus
- Ours
- Subtree
Most common types are Fast-Forward and Non Fast-Forward Recursive merge.
Merge moves HEAD forward but doesn't create new commit. We can say that the branch that call the merge is just updated.
The command git merge --squash branch_name allow us to group all the extra commits from branch_name to the stagging area of the current branch to make one single commit
- There are additional commits in the current branch and the branch we are trying to merge after this branch was created.
- An additional (merge) commit is cqreated in the current branch
- git merge --no-ff branch_name
Adding commits from other branch to de current branch. The result will be almost similar to a non fast-forward recursive merge. What happens is that we don't move de commits, we create new ones that are similar but with different ids. We shouldn't rebase commit outside our repository. Commands:
- git rebase other_branch
They usually happen when we work on the same file in different branches. When this occurs, we can:
- Accept current change: Accept the change that happened in the current branch
- Accept incoming change: Accept the change that happened in the incoming branch
- Accept both changes
- git diff: To see differences in both version of the merged branches
Use command git cherry-pick commit-id to add specific change (commit) in the current branch (this can come from any other branch). This will do a copy of the specific commit, meaning it will have a different commit id from the original
Useful to identify specific commits. It give an alias to a commit. Commands:
- git tag: give all the tags from the project
- git tag tag_name_or_number: It adds a tag refering to the current commit
- git tag tag_name_or_number commit_hash: It adds a tag refering to the specified commit
- git show tag_name_or_number:
- git tag -d tag_name_or_number: delete a tag
- git tag -a tag_name_or_number -m message: It creates and add a message to a tag
From github, your repositories, new repository
- git remote add remote_repo_name URL: To link the current local repository to a remote repository. The remote_repo_name is just an internal alias for the URL. Usually this name is origin.
- git remot show remote_repo_name: Detail information about the remote_repo_name
- git branch -M new_branch_name: To rename the current branch
- git push -u remote_repo_name_or_URL branch_name: Bring local changes to the remote repository. If the branch_name doesn't exists, it will be created. The -u flag is used just for the first branch_name commit of the repositry, usually the first commit in the master branch. The u flag create a conection with the the remote tracking branch, so it should be used whenever we push a new branch.
- git branch -a: List local branches and remote tracking branches.
- git branch -r: List remote tracking branches.
- git branch -r: List remote tracking branches.
- git branch --track branch_name remote_tracking_branch: Creates a local tracking branch based on the specified remote tracking branch
- git branch -vv: show last commit as well as which remote tracking branch is connected a local tracking branch
- git pull remote_repo_name branch_name: To update the remote tracking branch and merge it with the local branch.
- git fetch remote_repo_name branch_name: to update the remote tracking branch
- git clone URL: In an empty folder it clone a remote repository.
- git branch --delete --remote remote_tracking_branch: It delete a remote tracking branch.
- git push remote_repo_name --delete branch_name: It delete a branch in the remote repository
It grant access to a github account. This can be useful if we are working in an unfamiliar computer or if we are using the command line
Local read only copy of the remote branch. To update it we use the fetch command, to do this and merge it with our local branch in one step we use the pull command. It is created when we do our first branch push.
Similar to a remote tracking branch but this one can be edited. This branch is conected to the remote tracking branch. It MUST have the same name of the remote tracking branch