Version Control in General, Why?
merges files when there is more than one person working on the same file.
acts like a time capsule where you can check past commits if you ever need to go back.
Why is GIT better?
- It's a distributed repo. Everyone has a copy on their machines.
- It's not in a central repository.
- You can work on it offline.
git config - global user.name "Philip John Basile"
Setting the author of the code
git config - global user.email name@email.com
Setting the author's email
git config - global color.ui true
turn on pretty colors
git init
Initializes a Git repo in your chosen directory.
git status
Sees what the current state of our project is and what has changed.
git commit -m 'add cute octocat story'
Stores the saved changes with a message describing what was changed.
git add octocat.txt
Tells Git to start tracking changes made to octocat.txt
git add README.txt LICENSE
adds the two files README.txt and LICENSE to the staging area.
git add *.txt
Adds all txt files in current directory
git add docs/*.txt
Adds all txt files in docs directory
git add docs/
Adds all files in docs directory
git add '*.txt'
Adds all txt files in the whole project
git add - all
adds all files to the staging area.
git log
Git's journal that remembers all the changes we've committed so far in the order we committed them.
git remote add origin https://github.com/try-git/try_git.git
Pushes our local repo to the remote GitHub server.
git push -u origin master
The push command tells Git where to put our local commits when we're ready. The name of our remote is origin and the default local branch name is master. The -u tells Git to remember the parameters, so that next time we can simply run git push and Git will know what to do.
git pull origin master
Checks for changes on our GitHub repo and pulls down any new changes.
git diff HEAD
Views all of the merge conflicts. Since HEAD is used it diffs against our most recent commit.
git diff - staged
Lets you see the changes that were just staged.
git reset octofamily/octodog.txt
Lets you unstage files using the git reset command.
git checkout - octocat.txt
If you mess up, you can replace the changes in your working tree with the last content in head. Changes already added to the index, as well as new files, will be kept. This gets rid of all the changes since the last commit for octocat.txt.
git branch clean_up
Creates a copy of their code they can make separate commits to. Once done you merge this branch into the main master branch.
git branch
Lists the local branch(es).
git checkout clean_up
Switch from one branch to another.
git rm '*.txt'
This command will not only remove the actual files from disk, but will also stage the removal of the files for us.
git merge clean_up
Merges the changes from the clean_up branch into the master branch.
git branch -d clean_up
Delete a branch when you have merged its contents and do not need it anymore.
git push
Moves content to the remote repo.
If you enjoy my technology-focused articles and insights and wish to support my work, feel free to visit my Ko-fi page at https://ko-fi.com/philipjohnbasile. Every coffee you buy me helps keep the tech wisdom flowing and allows me to continue sharing valuable content with our community. Your support is greatly appreciated!
Top comments (7)
how is
git merge <branch-name>
different fromgit rebase <branch-name>
? Will themerge
just merge the files without creating a commit?merge will merge current branch to master. rebase will apply master onto the current.
The odd on out is rebase. A branch is a deviation from some commit. A base is the point the deviation occurred. To rebase is to change the commit where the branch starts to deviate. The short form is to rebase is where each change is reapplied (cherry-picked) an a new location.
We're supposed to add files to tracking everytime we commit right?
If you want those files tracked. Things like the node modules folder or notes should not be in there. And no api keys if it’s on a public git.
add
will put changes in your stage which is used to form a commit. New files will track, but the main operation isn't to track a file.Useful!