A note about learning Git.
Git
Git is a popular version control system.
Get started
Install
1 | add-apt-repository ppa:git-core/ppa |
Check the Git version
1 | git --version |
Configure git
1 | git config --global user.name "Your Name" |
Note:
--global
will set the name and email for every repository on this computer.
You can remove--global
to set the information just for this repository.
Create a git folder
1 | mkdir myproject |
Initialize git
1 | git init |
Note: Git creates a hidden folder called .git in the root directory of the repository to keep track of changes.
Add new files to git
- First, create a new file.
1 | git status |
Now git is aware of the file, but has not added it to our repository.
- Tracked: files have been added to your repository.
- Untracked: files that are in your working directory, but not added to your repository.
git status --short
can display the changes in a more compact way.
??
Untracked files.
A
Files add to stage.
M
Modified files.
D
Deleted files.
Use git diff <filename>
to show the differences.
Git staging environment
1 | git add <filename> |
Use git add -a
to add all files.
Git commit
git commit -m "<message>"
- Use
git commit -a -m "<message>"
to commit without stage(don’t recommend). - Use
git log
to view the historu of commits.
Git help
git commit --help
– See all the available options for the specific command.- Use
--help
to open the relevant Git manual page.
- Use
git help --all
– See all possible commands.SHIFT + g
to the end of the list,q
to exit.
Git branch
A branch is a new version of the main repository’s codebase.
To add some new features to the firstfile.cpp
, create a new branch.
1 | git branch newbranch |
git branch
can display all the branches.
git checkout newbranch
can move our current workspace from the main branch to the new branch.
git checkout -b newbranch
will create a new branch and move to it if it doesn’t exist.
Make some changes to a file and add a new file.
git status
– To check the status of the current branch.
git add --all
– Add files to the staging environment.
Now, git checkout main
and ls
, you’ll find that the file created just now is no longer exist.
Emergency branch
Imagine you are not yet done with the new branch, but we need to fix an error on main.
We can create a new branch called bugFix
to deal with it without messing the two branches.
Make some changes, git add
and git commit
, now we should merge two branches.
Git merge
Merge branches
Now git checkout main
and git merge bugFix
.
1 | git merge bugFix |
After merging, this branch is useless, so we want to delete it.
1 | git branch -d bugFix |
Merge conflicts
Now we return to our newbranch to finish our work, make some changes to the firstfile.cpp
.
Then git checkout main
and git merge newbranch
to apply new changes.
We failed, then use git status
to check the status.
1 | Unmerged paths: |
Open the firstfile.cpp
you’ll find there’s something new.
Adjust the file to what you want, then git add and git commit -m “merged changes”.
Delete the newbranch
, then finish this part of work.
Git and Github
Get started
Go to Github to sign up for an account and creat a new repository.
Remember to use the same e-mail adress you used in the git config.
Push local repository to GitHub
Copy the link of the repository paste the following command.
Search for how to configure ssh.
1 | git remote add origin git@github.com:lzlcs/learngit.git |
Now, push out main branch to the origin url, and set it as the default remote branch.
1 | git push --set-upstream origin main |
-u
is the same as--set-upstream
.
Edit files in Github
It’s simple, follow the instructions.
Git pull from github
Use pull
to get the most recent changes to your local copy.
pull
is a combination of 2 different commands: fetch
and merge
.
Git fetch
fetch
gets all the change history of a tracked branch.
Now check our status
.
1 | On branch main |
We are behind the origin/main
by 1 commit
, let’s double check by viewing the log
.
Also use git diff
to show the differences between our local main
and origin/main
.
So we can safely merge
.
Git merge
1 | git merge origin/main |
Everything is done.
Git pull
Make another change to the README.md
.
1 | git pull origin |
Push to Github
Try making some changes to our local git and pushing them to Github.
1 | git commit -m "xxxx" |
Go to Github to confirm that the repository has a new commmit.
Github branch
Create a branch beta
on Github and make some changes.
1 | git pull origin |
Use
git branch -a
to show all local and remote branches.
Usegit branch -r
to show remote branches only.
git checkout beta
and git pull
to check if it is all up to date.
Now you can see the changes you made recently on branch beta
.
Push a branch to Github
1 | git checkout -b branch2 |
Make some changes.
1 | git status |
In Github, we can see the changes and merge them into the branch master
if we approve it.
Github flow
- Create a new branch
- Make changes and add commits
- Open a pull request
- Review
- Deploy
- Merge
Create a new branch
If you want to try something new, you create a new branch.
Branching gives you an environment where you can make changes without affecting the main branch.
When you make a new branch, you will always want to make it from the main branch.
Using descriptive names for new branches, so everyone can understand what is happening.
Make changes and add commits
When you reach a small milestone, add the changes into your branch by commit.
Commit message are very important.
Open a pull request
A Pull Request notifies people you have changes ready for them to consider or review.
Review
When a Pull Request is made, it can be reviewed by whoever has the proper access to the branch.
Deploy
GitHub allows you to deploy from a branch for final testing in production before merging with the master branch.
Merge
After exhaustive testing, you can merge the code into the master branch!
Git contribute
Github fork
Fork a repository
Click the fork
.
Git clone
Clone a fork from Github
Move back to the origin repository.
1 | git clone .... |
Raname: git remote rename origin upstream
.
Then fetch the URL of our own fork: git remote add origin ...
- origin: we can read and write.
- upstream wo can read only.
Github send pull request
Make some changes.
1 | git add . |
Now go to Github and pull request.
Git ignore
When sharing your code with others, there are often files or parts of your project, you do not want to share.
Create .gitignore
touch .gitignore
It is also possible to have additional .gitignore files in subdirectories.
These only apply to files or folders within that directory.
Git security
1 | ssh-keygen -t rsa -b 4096 -C "...@...com" |
Add ssh to Github.
1 | ssh -T git@github.com |
Git undo
Git revert
When we want to take a previous commit and add it as a new commit, keeping the log intact.
1 | git log --oneline |
Use --no-edit
to skip the commit message editor (getting the default revert message).
Git reset
- Use
git log --pretty=oneline
to show the commit history. - Use
git reset --hard HEAD^
to be the last version.HEAD
is the current version,HEAD^
is the previous version.HEAD~n
is the previous n version
- Use
git reflog
to show your commands, you can find the version number to estoppel.
Git amend
commit --amend is used to modify the most recent commit.
1 | git commit -m "Adding plines to reddme" |
You should avoid making changes that rewrite history to remote repositories,
especially if others are working with them.