Push an existing repository

  1. Initialize an existing project to start tracking with git.

    • Go into the directory containing the project.
    • Type git init.
    • Type git add . to add all of the relevant files.
      • You’ll probably want to create a .gitignore file right away, to indicate all of the files you don’t want to track. Use git add .gitignore, too.
    • Type git commit.
  2. Create a online repo and connect it to your local git project..

    1. Go to GitHub, click the new repository button in the top-right. You’ll have an option there to initialize the repository with a README file, but I don’t.

      new GitHub repo

    2. Click the “Create repository” button.

    3. Connect your local repo to the remote repo you just created using the following cmds.

      # add repo name origin to the remote repo at the URL
      git remote add origin https://github.com/my1396/Damage-Function.git
      # rename the current local branch to main
      git branch -M main 
      # push update from the current local branch (main) to origin (remote) repo's main branch; -u is a shortname for --set-upstream; first parameter is upstream then 2nd parameter is local repo
      # local and remote branche names should be matching
      git push -u origin main 
      
    4. Then use GitHub Desktop to manage the repo committing, syncing, …, later on.

Documentation: https://git-scm.com/docs/git-push

commit how to see the detail of changes.

branch what does it do?

how could I check the change made by coauthors?

Do I need to approve their change?

pull will sync my local repository with the remote repository on Github?

Github common commands

git rev-parse --is-inside-work-tree check is a folder is a git repository. Which will print ‘true’ to STDOUT if you are in a git repos working tree.

  • Note that it still returns output to STDERR if you are outside of a git repo (and does not print ‘false’).

fetch downloads commits, files from a remote repository into your local repo, but it doesn’t integrate any of this new data into your working files.

pull download + merge. Directly integrates it into your current working copy files.

git rm The primary function of git rm is to remove tracked files from the Git index. Additionally, git rm can be used to remove files from both the staging index and the working directory.

  • -r the option is shorthand for ‘recursive’. When operating in recursive mode git rm will remove a target directory and all the contents of that directory.
  • --cached The -- separator option is used to explicitly distinguish between a list of file names and the arguments being passed to git rm. This is useful if some of the file names have syntax that might be mistaken for other options.
    • The cached option specifies that the removal should happen only on the staging index. Working directory files will be left alone.

Git doesn’t track directories, so it won’t remove ones that become empty as a result of a merge or other change. However, you can use git clean -fd to remove untracked directories (the -fdflag means force removal of untracked files and directories).

gitignore

foo/ will match a directory foo and paths underneath it. foo and /foo have the same effect. The leading slash doesn’t matter.

* mathches anything (zero, one, or more characters) except a slash /.

? mathces one single character except a slash /.

A line starting with # serves as a comment. Put a backslash (“\”, escape character) in front of the first hash for patterns that begin with a hash.

Delete a file in .gitignore after you have already added it to the repo.

How to make Git forget about a file that was tracked, but is now in .gitignore?

.gitignore is only for untracked files.

# This removes all files from the repository and adds them back (this time respecting the rules in your .gitignore).
git rm -rf --cached "Shared folder.Rproj"
git add .
git commit -m "clear cache"
git push

If you make changes to your repository, the workflow is add $\rightarrow$ commit $\rightarrow$ push.

Undo local changes

Until you push your changes to a remote repository, changes you make in Git are only in your local development environment.

When you make a change, but have not yet staged it, you can undo your work.

git reset --hard

Undo staged local changes:

git reset --hard

Undo committed local changes. When you commit to your local repository (git commit), Git records your changes. Because you did not push to a remote repository yet, your changes are not public (or shared with other developers). At this point, you can undo your changes.

Failure When Push Large Files

Github Error:

This is an HTTP buffer issue. Happens when you are pushing a large amount of data.

Fix:

  1. Increase the buffer will solve the issue. [Easiest solution] Or,
  2. you could push by small batches of changes. Or,
  3. use Git Large File Storage.
git config http.postBuffer 524288000
git pull && git push

Q: what does http.postBuffer do?

A: This option changes the size of the buffer that Git uses when pushing data to a remote over HTTP or HTTPS.

The default of httpBuffer size is set to 1MB for https. Please note the only acceptable values are 524288000 (500mb), 1048576000 (1 GB) and 2147483648 (2 GB). Anything above it, is considered out of range.

git config http.postBuffer 524288000 will set the httpBuffer size to 500 MB.

To remove the file that you have already committed, you are going to need to reset your HEAD to the commit before the one that contains your file. Make sure you are performing a soft reset.

git reset --soft HEAD~1

1 is the number of commits you need to move back, can be greater than 1.


Git Large File Storage

./install.sh. install Git Large File Storage from source.

Go to the Git repository where you want to use Git LFS, select the file types you’d like Git LFS to manage (or directly edit your .gitattributes). You can configure additional file extensions at anytime.

git lfs track "*.psd"
git lfs track "*.png"

Now make sure .gitattributes is tracked. Your tracked files’ details are saved inside a .gitattributesso make sure to add .gitattributes to persist tracking when other users clone the project.

git add .gitattributes

You should then be able to safely add, commit and push!

git add file.png
git commit -m "Add design file"
git push origin main

Branch management

You can also open Pull Requests between separate branches on GitHub. This often presents a good way for collaborating with people who have access to the same repository. You don’t want to all be pushing to the main branch all the time. Instead, each person can create their own branch, work separately, and then open a pull request to merge that branch into main.

If you create a local branch in your repo, you can push it to GitHub as follows:

First, make sure that you are on the branch that you want to push:

$ git branch

Then run

# -u option will set up a link between local and remote branches
$ git push -u origin <branch_name>

to push the current local (active) branch to the remote branch <branch-name>. Usually branch names should be matching between local and remote.

git branch will print a list of branches linked to the current repo. * prefix the branch that you have currently checked out (i.e., the branch that HEAD points to).

  • could be used to check which branch I am currently on.

  • git branch Menghan create a branch called Menghan

  • git branch -d Menghan delete the local branch Menghan

  • git branch -r show remote branches. Remote branched are just like local branches, except they map to commits from somebody else’s repository. Remote branches are prefixed by the remote they belong to so that you don’t mix them up with local branches.

    MY-Nuffield:Shared folder Menghan$ git branch -r
      origin/HEAD -> origin/master
      origin/Menghan
      origin/coauthor
      origin/master
    
  • git branch -M <main> rename the current branch to main.

git push <remote> <branch> Push commits made on your local branch to a remote rep.

  • E.g., git push origin main push your local changes to the remote main branch.
    • Where git push initiates the push, origin refers to the remote counterpart of the project, and main is the branch name. This is common when you are the only contributor to your project, and you want to directly edit the default branch of your project with changes.
  • <remote> is the destination remote repo name of a push operation. This parameter can be either a URL or the name of a remote.
    • When the command line does not specify where to push with the <remote> argument, branch.*.remote configuration for the current branch is consulted to determine where to push. If the configuration is missing, it defaults to origin.
  • <branch> is a branch name.

git push origin <branch> will push the current branch to the remote counterpart of that branch.

git push origin will push the current branch to the branch of the matching name in the remote repository (aka, “branch configured upstream”), if it exists. Otherwise, it will not push and notify that the current branch has no remote counterpart (error message: “ has no upstream branch”).

  • The default branch in your project is conventionally a branch named “main”. This branch is the version of the project that goes into production or the version from which you will create further branches to isolate changes, and merge back into the default branch.

  • If a project you are working on is older, the default branch might be named “master”, which GitHub changed to remove references to slavery in conventional terminology. It’s important to check the name of the default branch.

git push origin flags

  • -u, or --set-upstream:

    git push -u origin remote-branch This creates a remote branch and sets it upstream of the current branch you are pushing. The relationship between the current branch and upstream branch is remembered, such that you will not have to continually connect the remote and local branches when pushing commits.

    • It is recommended to use -u flag for the first push on a specific branch. When you use the -uflag, Git will create a link between your local branch and the remote branch.
    • Once a link btw your local and remote branches is crated, you don’t need to specify repo and branch in the future pull and push, Git will remember which remote branch corresponds to your local branch.
  • -f, --force: Pushes that would delete or overwrite existing code are usually blocked. With this command, pushes from your local repository would be forced onto the remote repository, potentially deleting or overwriting other commits!

  • -d, --delete: Deletes the remote branches listed. Eg, git push origin --delete <branch name>

  • --all: Pushes all local branches to remote repository

Solve the no upstream branch error message: create a remote branch with the same name as the local branch and push changes to the remote branch (aka, “set upstream”).

  • $ git push --set-upstream origin .

Merge

git merge <source> command lets you take the independent lines of development created by git branch and integrate them into a single branch.

To do a merge (locally), git checkout the branch you want to merge INTO. Then type git merge <branch> where <branch> is the branch you want to merge FROM.

git checkout main # switch to the main branch
git merge new-feature # merge updates in new-feature to main

image-20230717104452192

When creating a merge commit Git will attempt to auto magically merge the separate histories for you. If Git encounters a piece of data that is changed in both histories it will be unable to automatically combine them. This scenario is a version control conflict and Git will need user intervention to continue.

Resolve conflicts: git mergetool to check where the conflict occurs and why it occurs.

  • To see that which is the first edited text of the merge conflict in your file, search the file attached with conflict marker «««<.

    • You can see the changes from the HEAD or base branch after the line «««< HEAD in your text editor.

    • Next, you can see the divider like =======. It divides your changes from the changes in the other branch, followed by »»»> BRANCH-NAME. In the above example, user1 wrote “<h1> Git is a version control</h1>” in the base or HEAD branch and user2 wrote “<h2> Git is a version control system </h2>”.

      image-20230717112204536

    • Decide whether you want to keep only your branch’s changes or the other branch’s changes, or create a new change. Delete the conflict markers «««<, =======, »»»> and create final changes you want to merge.

To accept the changes, use the rebase command. git rebase --continue

git checkout <Menghan> switch to the branch Menghan.

  • git checkout . will forgo all unstaged changes.
  • git checkout -b <new-branch> create a new branch named new-branch and then checked out.

git branch (-m | -M) [<oldbranch>] <newbranch> <oldbranch> will be renamed to <newbranch>. If <oldbranch> had a corresponding reflog, it is renamed to match <newbranch>, and a reflog entry is created to remember the branch renaming. If <newbranch> exists, -M must be used to force the rename to happen.

git remote add <name> <url> Add a remote named <name> for the repository at <url>. This is used to configure the remote repo, created a reference using the repo <url>.

  • <name> is a short remote name for your reference.

  • Now you can pass that remote name <name> to git fetch <name> to download the contents.

Fetch a specific branch use git fetch <remote repo name> <branch name>. E.g., git fetch origin test.

Difference of fetch from pull: fetch does not merge; pull automatically merge remote to the current branch.

git update-index --skip-worktree <file> causes the following error

# The following pathspecs didn't match any eligible path, but they do match index
entries outside the current sparse checkout:
  • If what you want to do is to remove that <file> and index entry, unset the skip-worktree flag first, with git update-index --no-skip-worktree "Shared folder.Rproj", and then git rm will work as expected.

  • If what you want to do is to just remove the index entry, you can do that directly, at the core-command level, git update-index --force-remove "Shared folder.Rproj", or unset the flag as above then git rm --cached it.

git push origin --delete Menghan delete a remote branch.

git push -u origin Menghan push updates from Menghan (local) to origin (remote) branch. -u is equivalent to --set-upstream.

Check history on Github

Code $\rightarrow$ History $\rightarrow$ click one commit, this will show your revision history.

Local repository content will change according to which branch you checkout in terminal. You make changes at your local branch, then you merge the updates to the master branch.

If your branch is ahead of your master — You get that message because you made changes in your local master and you didn’t push them to remote. You need to navigate to master and merge the change from the local branch.

Use the following code to fix the ahead problem.

git add -A
git commit -m "My commit"
git checkout master # have to first checkout to master [receiving branch]
git merge Menghan # merge from Menghan --> master
git push -u origin master # push master changes from the current branch to the remote repo master branch.

If your branch is behind your master: navigate to the local branch and merge change from master.

git checkout master # you are switching your branch to master
git pull # pull update from master
git checkout Menghan # switch back to your branch [receiving branch]
git merge master # from master --> Menghan

After merging it, check if there is a conflict or not. If there is NO CONFLICT then:

git push

If there is a conflict then fix your file(s), then:

git add yourFile(s)
git commit -m 'updating my branch'
git push

To pull a repository from remote

cd path_for_git_folder
git clone URL